实体框架协会与非关键字段 [英] Entity Framework Association with Non Key fields

查看:91
本文介绍了实体框架协会与非关键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在实体框架中创建关联b / t 2个非关键字段?



示例:在遗留应用程序中使用2个表(即键/结构不能更改)



订单 - OrderId:int:PK - OrderNo:varchar



OrderDetails - DetailRecordId:int:PK - OrderNo:varchar



在实体框架中,我想通过OrderNo字段创建一个关联b / t Order和OrderDetails,它不是表中的主键或数据库中的FK关系。


$ b $这对我来说似乎不仅应该是容易做的,而且还有一些使用EF的原因。但是,似乎只允许我使用实体键创建关联。

解决方案

实体框架允许您>声明,这些列是键,并且FK约束存在于数据库中没有实际存在。



这是因为SSDL(EDMX的StorageModel部分)如果有必要,可以对数据库进行操作和谎言



然后,EF将与数据库进行交互,就像键和外键真的存在。



这应该有效,但是关于参照完整性的所有正常注意事项都适用。



实体框架提示



希望这有帮助。


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 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.

解决方案

The Entity Framework allows you to claim that columns are keys and that FK constraints exist where none actually exist in the database.

That is because the SSDL (StorageModel part of the EDMX) can if necessary be manipulated by you and lie about the database.

The EF will then interact with the database as if the keys and foreign keys really do exist.

This should work, but all the normal caveats about referential integrity apply.

See my Entity Framework Tips

Hope this helps.

这篇关于实体框架协会与非关键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆