开发者

Foreign key contraints interpretation problem

开发者 https://www.devze.com 2023-03-07 06:11 出处:网络
I generate Entity Data Model in Visual studio from the database. However, I noticed that it does not generate 开发者_运维技巧neither relationships nor navigation properties from some foreign keys.It

I generate Entity Data Model in Visual studio from the database.

However, I noticed that it does not generate 开发者_运维技巧neither relationships nor navigation properties from some foreign keys. It occurs when the foreign key contsraints are defined with the disabled option Enforce Foreign Key Constraint, like in the following exhibit (from SSMS).

Is there any way to deal with this? Unfortunately I cannot alter my database schema.

Foreign key contraints interpretation problem


You can add them manually from the designer but it can have some consequences depending on the version of entity framework yo are using.

If you simply add association from the toolbox it by default creates independent association. Independent association must be mapped to the database counterpart. It means that you must manually open EDMX and cheat EF by modifying SSDL (you will add relation to SSDL part manually which can be quite hard task - follow SSDL reference: AssociationSet and Association elements). Now you can map the relation in the Mapping details window of the designer (you will also have to modify entities because FK property mustn't be mapped to the entity when independent association is used and in case of many-to-many association you will have to remove entity for junction table). EF will believe that this relation exists in the database. Once you modify SSDL manually you cannot use Update from the database any more. It will delete all your manual changes.

If you are using EFv4 you can use foreign key association (complete differences between those two types is described here). Foreign key association is not mapped but it cannot be used to define many-to-many relation.

The problem will occur if data in the database doesn't enforce the relation (which can happen because FKs are disabled). In such case your application will not work and there will be no way around this except repairing data integrity in the database or removing the association from the model.

The best solution for you is turning on FKs in the database!


Unfortunately You have to add those by hand in the model. That's the power of OR Mapping. Model can look different (better) than database.

0

精彩评论

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