假设我有两个实体对象“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 声明的异常。