2

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.

enter image description here

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
Func
  • 443
  • 3
  • 5
  • 10

2 Answers2

1

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!

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
0

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.

Piotr Perak
  • 9,824
  • 9
  • 42
  • 75