RAM*_*RAM 3 c# entity-framework code-first
我有以下实体:
public class Notification
{
public int Id { get; set; }
public string Title { get; set; }
public Guid RefId { get; set; }
public Object Ref { get; set; } // << The navigation property: Sometime its type is Poll and sometime is Test, maybe I add other types too
public NotifTypes Type { get; set; }
}
public enum NotifTypes
{
Poll=1,
Test=2,
// Other NotifTypes here
}
//-------------------------------------------------------------------
public class Test
{
public int Id { get; set; }
public string Title { get; set; }
public IEnumerable<Notification> { get; set; }
}
public class Poll
{
public int Id { get; set; }
public string Answer1 { get; set; }
public string Answer2 { get; set; }
public IEnumerable<Notification> { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
好的,
Type的属性Notification相等时Poll,RefId将用a填充PollIdTest,refId将用 a 填充TestId。现在我想有条件地包含相关Poll或Testin Refproperty。我应该如何实施?
我想防止添加单独的 ID,例如
PollId、TestId和...,Notification因为我确信每次只有其中一个具有价值,所以我希望拥有一个RefId和一个Ref属性而不是它们。
我不知道EntityFramework,但你让我回答这个问题。
您基本上是在重新发明多态关联,这不是一个好的关系设计。您可以阅读我过去关于这个概念的一些回答:
我倾向于回答 MySQL 问题,但对于任何其他品牌的 RDBMS 来说答案都是一样的。您无法声明引用多个表的实际外键约束这一事实应该表明此设计不正确。
从数据建模的角度来看,最简单的解决方案是为每个潜在的表引用创建一个独立的属性。在给定行上,除了其中一个之外,所有这些都将为 NULL。
我不知道 EntityFramework 如何支持这一点。@AluanHaddad 的建议听起来不错。
尽量不要打破关系概念。沿着这条路就是“内部平台效应”反模式。
| 归档时间: |
|
| 查看次数: |
435 次 |
| 最近记录: |