让 EF 使用 m:n table 和额外的 属性
Make EF use a m:n table with additional property
我 运行 在创建新应用时遇到问题。该应用程序是关于在特定日期进入的新员工。由于他们需要一些硬件(例如笔记本电脑、键盘等),我们想提供一下未来几天将要来的所有新员工的概况。
所以有以下型号:
条目:
public class Entry{
public IEnumerable<Hardware> Hardware {get; set;}
...
}
硬件:
public class Hardware{
public string Name {get; set;}
public OrderStatus Status {get; set}
}
订单状态:
public enum OrderStatus{
Nothing,
Ordered,
Arrived,
Ready
}
起初这似乎很好,因为我想为每个条目创建一个新的硬件数据集(一对多)。现在我决定维护一个硬件列表,并只将硬件引用到条目(多对多)。问题是 OrderStatus 绑定到硬件,因此当一个硬件被许多员工引用并且 Orderstatus 发生更改时,它将在所有条目上更改。
我认为正确的解决方案是使用第三个 table(Entry_Id、Hardware_Id、OrderStatus)。使用 fluent API 可以在新的(第三个)table 中映射关系,但不可能向其添加新的 属性 (OrderStatus)。这可能吗?
根据这个answer:
It's not possible to create a many-to-many relationship with a
customized join table. In a many-to-many relationship EF manages the
join table internally and hidden. It's a table without an Entity class
in your model. To work with such a join table with additional
properties you will have to create actually two one-to-many
relationships
请参阅链接的答案了解更多详情。
我 运行 在创建新应用时遇到问题。该应用程序是关于在特定日期进入的新员工。由于他们需要一些硬件(例如笔记本电脑、键盘等),我们想提供一下未来几天将要来的所有新员工的概况。
所以有以下型号:
条目:
public class Entry{
public IEnumerable<Hardware> Hardware {get; set;}
...
}
硬件:
public class Hardware{
public string Name {get; set;}
public OrderStatus Status {get; set}
}
订单状态:
public enum OrderStatus{
Nothing,
Ordered,
Arrived,
Ready
}
起初这似乎很好,因为我想为每个条目创建一个新的硬件数据集(一对多)。现在我决定维护一个硬件列表,并只将硬件引用到条目(多对多)。问题是 OrderStatus 绑定到硬件,因此当一个硬件被许多员工引用并且 Orderstatus 发生更改时,它将在所有条目上更改。
我认为正确的解决方案是使用第三个 table(Entry_Id、Hardware_Id、OrderStatus)。使用 fluent API 可以在新的(第三个)table 中映射关系,但不可能向其添加新的 属性 (OrderStatus)。这可能吗?
根据这个answer:
It's not possible to create a many-to-many relationship with a customized join table. In a many-to-many relationship EF manages the join table internally and hidden. It's a table without an Entity class in your model. To work with such a join table with additional properties you will have to create actually two one-to-many relationships
请参阅链接的答案了解更多详情。