开发者

Entity Framework Association b/t non-keys

开发者 https://www.devze.com 2022-12-11 17:02 出处:网络
Is it possible to create associates b/t 2 non-key fields in the Entity Framework? Example:Take the 2 tables in a legacy application (i.e. keys/structure cannot change)

Is it possible to create associates b/t 2 non-key fields in the Entity Framework?

Example: Take the 2 tables in a legacy application (i.e. keys/structure cannot change)

Order - OrderId : int : PK - OrderNo : varchar

OrderDetails - DetailRecordId : int : PK - OrderNo : varchar

In the Entity Framework, I want to create an association b/t Order and OrderDetails开发者_Python百科 by the OrderNo field, which is not a primary key on either table or a FK relationship in the database.

This seems to me as not only should it be easy to do, but one reasons to use something like EF. However, it seems to only want to allow me to create associations using entity keys.


Without any sort of constraints on the OrderNo field in either relation schema, EF is going to have a hard time handling the navigation property (i.e.: OrderNo is unique? cardinality of the relation?)

OrderNo is considered a non-identifying property and hence makes navigation properties a bit difficult to "auto-gen" up.

To achieve what you want you can use a simple Linq-to-Entity command to fetch what you need from a given entity. The command can then be abstracted behind a Property declared in the Entity's partial class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号