Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF,如何有条件地包含与另一个属性的值相关的导航属性类型?_C#_Entity Framework_Code First_Polymorphic Associations - Fatal编程技术网

C# EF,如何有条件地包含与另一个属性的值相关的导航属性类型?

C# EF,如何有条件地包含与另一个属性的值相关的导航属性类型?,c#,entity-framework,code-first,polymorphic-associations,C#,Entity Framework,Code First,Polymorphic Associations,我有以下实体: 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 some

我有以下实体:

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; }
}
公共类通知
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共Guid RefId{get;set;}

公共对象Ref{get;set;}/我不知道EntityFramework,但您让我回答这个问题

你基本上是在重新设计一个不好的关系设计。你可以阅读我过去关于这个概念的一些答案:

我倾向于回答MySQL的问题,但对于任何其他品牌的RDBMS,答案都是一样的。不能声明引用多个表的实际外键约束这一事实应该是这种设计不正确的线索

从数据建模的角度来看,最简单的解决方案是为每个潜在的表引用创建一个独立的属性。在给定的行中,除了一个之外,所有这些都将为NULL

我不知道EntityFramework如何支持这一点。@AluanHaddad的建议听起来不错


尽量不要打破关系概念。沿着这条路走下去的是。

这不是一个好的设计,我很确定你不可能让它与EF一起工作。你应该有一个单独的实体(一个
通知
指向)它具有到相关表的链接。仅当
Poll
Test
具有公共基类型并且通过DB继承(如TPH或TPT)映射到数据库时,这才有效。此外,您的
NotifTypes
enum是problematic@AluanHaddad你能在回答中解释一下你在评论中所写内容的实施情况吗ean
类通知{public string Title{get;set;}public int Id{get;set;}}
然后
类通知{public Poll Poll Poll{get;set;}}
类通知{public Test Test Test{get;set;}
类通知{public ICollection通知{get;set;}}=new();}
等。然后删除
NotifTypes
,如果您直接查询通知,您可以从pn db.notifications.OfType()中写入
,其中pn.Poll.Answer1==“Biden或Trump”选择pn
;谢谢您的清晰回答。