Entity Framework 使用数据注释的 Code First 3 方式实体连接
Entity Framework Code First 3 way entity connection using data annotation
我正在尝试使用 EF Code-First 定义 3 个实体之间的连接。
描述连接的最佳方式是使用此 class 图:
我的想法是在 Tab
中包含许多 Shapes
和 Actions
。
每个 Shape
由 Actions
定义,created\modified 它并与一个 Tab
相关联。
每个动作都在特定的 Tab
中执行,并且正在操纵 Shape
(创建\重命名\更新\删除)。
我已经设法用代码创建了这个图表,没有 Shape
和 Tab
之间的连接。这就是我卡住的地方。
这是我的代码:
选项卡
public class Tab
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public virtual ICollection<VAction> Actions { get; set; }
public virtual ICollection<Shape> Shapes { get; set; }
}
动作
public class VAction
{
[Key, Column(Order = 0)]
public string ID { get; set; }
[ForeignKey("Tab")]
[Key, Column(Order = 1)]
public int TabID { get; set; }
public string ShapeLbl { get; set; }
public virtual Tab Tab { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; }
[ForeignKey("ShapeLbl, TabID")]
public virtual Shape Shape { get; set; }
}
形状
public class Shape
{
[Key, Column(Order=0)]
public string Lbl { get; set; }
[ForeignKey("Tab")]
[Key, Column(Order = 1)]
public int TabID { get; set; }
public virtual Tab Tab { get; set; }
public virtual ICollection<VAction> Actions { get; set; }
}
目前我遇到这个错误:
One or more validation errors were detected during model generation:
Shape_VAction: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'TabID' on entity 'VAction' does not match the type of property 'Lbl' on entity 'Shape' in the referential constraint 'VAction_Shape'.
Shape_VAction: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ShapeLbl' on entity 'VAction' does not match the type of property 'TabID' on entity 'Shape' in the referential constraint 'VAction_Shape'.
如果您查看它试图在 TabID 和 Lbl 之间绑定的错误,我真的不明白为什么,因为我没有正确定义顺序。任何人都可以详细说明那部分吗?
将不胜感激!
更新 - 已解决
我不知道它如何以及为何重要,但自从我看到错误与订单有关。我只是尝试在两端替换它,而且它出人意料地有效。
我从:
操作:
[ForeignKey("ShapeLbl, TabID")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 0)]
public string Lbl { get; set; }
[Key, Column(Order = 1)]
public int TabID { get; set; }
收件人:
操作:
[ForeignKey("TabID, ShapeLbl")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 1)]
public string Lbl { get; set; }
[Key, Column(Order = 0)]
public int TabID { get; set; }
我不知道为什么这次更改有效而之前的更改无效。如果有人知道为什么我很想听到答案,因为我只是白白浪费了几个小时,至少我知道为什么。
更新 - 已解决
我不知道它如何以及为何重要,但自从我看到错误与订单有关。我只是试着把它两端都换掉,结果出乎意料地有效。
我从:
操作:
[ForeignKey("ShapeLbl, TabID")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 0)]
public string Lbl { get; set; }
[Key, Column(Order = 1)]
public int TabID { get; set; }
收件人:
操作:
[ForeignKey("TabID, ShapeLbl")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 1)]
public string Lbl { get; set; }
[Key, Column(Order = 0)]
public int TabID { get; set; }
我不知道为什么这次更改有效而之前的更改无效。如果有人知道为什么我很想听到答案,因为我只是白白浪费了几个小时,至少我知道为什么。
我正在尝试使用 EF Code-First 定义 3 个实体之间的连接。
描述连接的最佳方式是使用此 class 图:
我的想法是在 Tab
中包含许多 Shapes
和 Actions
。
每个 Shape
由 Actions
定义,created\modified 它并与一个 Tab
相关联。
每个动作都在特定的 Tab
中执行,并且正在操纵 Shape
(创建\重命名\更新\删除)。
我已经设法用代码创建了这个图表,没有 Shape
和 Tab
之间的连接。这就是我卡住的地方。
这是我的代码:
选项卡
public class Tab
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public virtual ICollection<VAction> Actions { get; set; }
public virtual ICollection<Shape> Shapes { get; set; }
}
动作
public class VAction
{
[Key, Column(Order = 0)]
public string ID { get; set; }
[ForeignKey("Tab")]
[Key, Column(Order = 1)]
public int TabID { get; set; }
public string ShapeLbl { get; set; }
public virtual Tab Tab { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; }
[ForeignKey("ShapeLbl, TabID")]
public virtual Shape Shape { get; set; }
}
形状
public class Shape
{
[Key, Column(Order=0)]
public string Lbl { get; set; }
[ForeignKey("Tab")]
[Key, Column(Order = 1)]
public int TabID { get; set; }
public virtual Tab Tab { get; set; }
public virtual ICollection<VAction> Actions { get; set; }
}
目前我遇到这个错误:
One or more validation errors were detected during model generation:
Shape_VAction: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'TabID' on entity 'VAction' does not match the type of property 'Lbl' on entity 'Shape' in the referential constraint 'VAction_Shape'.
Shape_VAction: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ShapeLbl' on entity 'VAction' does not match the type of property 'TabID' on entity 'Shape' in the referential constraint 'VAction_Shape'.
如果您查看它试图在 TabID 和 Lbl 之间绑定的错误,我真的不明白为什么,因为我没有正确定义顺序。任何人都可以详细说明那部分吗?
将不胜感激!
更新 - 已解决
我不知道它如何以及为何重要,但自从我看到错误与订单有关。我只是尝试在两端替换它,而且它出人意料地有效。
我从:
操作:
[ForeignKey("ShapeLbl, TabID")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 0)]
public string Lbl { get; set; }
[Key, Column(Order = 1)]
public int TabID { get; set; }
收件人:
操作:
[ForeignKey("TabID, ShapeLbl")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 1)]
public string Lbl { get; set; }
[Key, Column(Order = 0)]
public int TabID { get; set; }
我不知道为什么这次更改有效而之前的更改无效。如果有人知道为什么我很想听到答案,因为我只是白白浪费了几个小时,至少我知道为什么。
更新 - 已解决
我不知道它如何以及为何重要,但自从我看到错误与订单有关。我只是试着把它两端都换掉,结果出乎意料地有效。
我从:
操作:
[ForeignKey("ShapeLbl, TabID")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 0)]
public string Lbl { get; set; }
[Key, Column(Order = 1)]
public int TabID { get; set; }
收件人:
操作:
[ForeignKey("TabID, ShapeLbl")]
public virtual Shape Shape { get; set; }
形状:
[Key, Column(Order = 1)]
public string Lbl { get; set; }
[Key, Column(Order = 0)]
public int TabID { get; set; }
我不知道为什么这次更改有效而之前的更改无效。如果有人知道为什么我很想听到答案,因为我只是白白浪费了几个小时,至少我知道为什么。