gpt4 book ai didi

sql - 在 Entity Framework (SQL Server)中做 "either/or"关系的好方法

转载 作者:行者123 更新时间:2023-12-04 22:35:19 24 4
gpt4 key购买 nike

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

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

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

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

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

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

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

编辑:

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

当前行为示例:

回应@morganppdx 的评论:

鉴于此实体图:

enter image description here

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

最佳答案

在我的脑海中,我的建议是创建扩展 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 对象的接口(interface),但这些对象是用于谨慎目的的谨慎对象。

关于sql - 在 Entity Framework (SQL Server)中做 "either/or"关系的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5968796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com