4

假设我有两个实体对象“table”和“chicken”。

现在假设,我有一个“机翼”对象,我希望该机翼与桌子和鸡有 0..1-1 的关系。换句话说,我想要一个可以为空的 table.wing 和一个可以为空的 chicken.wing。

有没有一种使用 Entity Framework 4 的好方法来使机翼对象具有可以与桌子或鸡相关联的限制?

注意:我不想在我的字典中有一个有翼对象的基类——这需要是“有一个”而不是“是一个”。

我的想法是我不能对引用的集合做出独特的限制,所以我必须用类似的东西包装 Entity 属性:

public partial class Wing:
...
  public Table Table
    {
      get { return this.Table; }
      set { 
          //make sure Chicken is null
          this.Table = value;
          }
    }
...
}

这让我觉得很老套而且不太干净,所以我一直在寻找一个更好的(如果不是最好的)实践解决方案。

编辑:

需要明确的是,我目前在桌子和翅膀之间有 0..1-1 的关系,在鸡和翅膀之间有 0..1-1 的关系。因此,我可以创建一个 table.wing,然后我可以查看wing.table。我想要的是确保我在查询 table.wing.chicken 或 chicken.wing.table 时总是有一个空值。翼必须与一张桌子或一个翼相关联。

当前行为示例:

回应@morganppdx 的评论:

鉴于此实体图:

在此处输入图像描述

Program.cs 中的以下内容:

class Program
{  
 static void Main(string[] args)
    {
        Model1Container container = new Model1Container();

        Wing chickenwing = new Wing { Shape = "birdlike" };
        Chicken chicken1 = new Chicken { Breed = "Andalusian", Wing = chickenwing };
        Table table1 = new Table { Style = "Mission", Wing = chickenwing }; // Should throw exception!
        container.AddToChickens(chicken1);
        container.AddToTables(table1);
        container.SaveChanges();

        Console.Write(String.Format("Table {0}'s wing has a {1} shape...", table1.Id, table1.Wing.Shape));
        Console.Write(String.Format("Table {0} has {1} chicken wings!", table1.Id, table1.Wing.Chicken.Breed));
        Console.ReadLine(); //wait for input to give us time to read
    }
}

结果控制台将显示:

Table 1's wing has a birdlike shape...Table 1 has Andalusian chicken wings!

这个结果是我希望避免的。当 chickenwing 与 table1 关联时,它应该抛出异常,因为它已经与 chicken1 关联,并且不能同时与 table 和鸡关联。

很有可能我建立的关系不正确,因此没有在我想要的地方得到@morganpdx 声明的异常。

代码位于:https ://github.com/mettadore/WingThing

4

2 回答 2

1

在我的脑海中,我的建议是创建扩展 Wing 对象的子对象,并使用它们来代替你的 Wing 对象:

public class ChickenWing : Wing
{
  public Table Table { get { throw new NoTablesAllowedException; }}
}

public class TableWing: Wing
{
  public Chicken Chicken { get { throw new NoChickensHereException; }}
}

您发布的代码将如下所示:

class Program
{          
    static void Main(string[] args)           
    {               
        Model1Container container = new Model1Container();                      
        ChickenWing chickenwing = new ChickenWing { Shape = "birdlike" };
        TableWing tablewing = new TableWing { Shape = "circular" };
        Chicken chicken1 = new Chicken { Breed = "Andalusian", Wing = chickenwing };               
        Table table1 = new Table { Style = "Mission", Wing = tablewing };             
        container.AddToChickens(chicken1);               
        container.AddToTables(table1);               
        container.SaveChanges();                      

        Console.Write(String.Format("Table {0}'s wing has a {1} shape...", table1.Id, table1.Wing.Shape));               
        Console.Write(String.Format("Table {0} has {1} chicken wings!", table1.Id, table1.Wing.Chicken.Breed));               
        Console.ReadLine(); //wait for input to give us time to read           
    }       
} 

到目前为止,我还没有做过这样的事情,但我相信这应该会奏效。本质上,Wing 对象充当描述 ChickenWing 和 TableWing 对象的接口,但这些对象是用于谨慎目的的谨慎对象。

于 2011-05-11T20:10:11.553 回答
0

查看您的模型,我认为您可以简单地将 Table 和 Wing Ids 设置为具有不同种子和增量 2 的 IDENTITY - 一个将只有偶数和第二个奇数 Id,在这种情况下,永远不会有与两者相关的机翼。

关键是 EF 中的一对一关系始终建立在主键上,因此wing 必须具有 table 或 chicken 的主键,并且在定义独占序列时,wing 永远不会同时拥有 table 和 chicken。

于 2011-05-11T21:25:44.230 回答