EF,如何有条件地包含一个导航属性,该导航属性的类型与另一个属性的值有关? [英] EF, How to conditionally include a navigation property that type of it related to value of another property?

查看:72
本文介绍了EF,如何有条件地包含一个导航属性,该导航属性的类型与另一个属性的值有关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

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; }
}

好,


  • 通知对象的类型属性等于轮询 RefId 将由 PollId

  • 当类型等于 Test refId 将由 TestId 填充。

  • When the Type property of Notification object is equal Poll, the RefId will fill by a PollId
  • When type is equal Test, the refId will fill by a TestId.

现在我要有条件地包括相关的投票 Test 引用属性中。我应该如何实现它?

Now I want conditionally include the related Poll or Test in Ref property. How should I implement it?


我想防止添加单独的ID,例如 PollId TestId 和....到 Notification ,因为我确定每次只有其中一个有价值,所以我想一个 RefId 和一个 Ref 属性代替它们。

I want prevent to add separate Ids like PollId, TestId and.... to Notification because I'm sure that each time just one of them has value, so I want have one RefId and one Ref property instead of them.


推荐答案

我不知道EntityFramework,但是您要求我回答这个问题。

I don't know EntityFramework, but you asked me to answer this.

您基本上是在重塑 polymorphic-associations 不是良好的关系设计。您可以阅读我过去有关此概念的一些答案:

You're basically reinventing polymorphic-associations which is not a good relational design. You can read a few of my past answers about this concept:

  • Possible to do a MySQL foreign key to one of two possible tables?
  • Why can you not have a foreign key in a polymorphic association?
  • MySQL - Conditional Foreign Key Constraints

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

I tend to answer MySQL questions, but the answer is the same for any other brand of RDBMS. The fact that you cannot declare an actual foreign key constraint that references multiple tables should be a clue that this design is not right.

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

The easiest solution from a data modeling perspective is to create an independent attribute for each of your potential table references. All but one of these will be NULL on a given row.

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

I have no idea how EntityFramework might support this. @AluanHaddad's advice sounds good.

尽量不要破坏关系概念。沿着这条路的是内部平台效应反模式

Try not to break relational concepts. Down that path is the Inner-Platform Effect antipattern.

这篇关于EF,如何有条件地包含一个导航属性,该导航属性的类型与另一个属性的值有关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆