Oleg Sh
Oleg Sh

Reputation: 9013

Entity Framework - stange behaviour

I have 3 simplest tables:

http://magicscreenshot.blob.core.windows.net/screenshots/udYWngOVQEM.jpg

and relationships among them:

http://magicscreenshot.blob.core.windows.net/screenshots/zTK28gBgkEc.jpg

If I create a new project from zero and create Entity Model (from DB) I receive correct EF model:

http://magicscreenshot.blob.core.windows.net/screenshots/ZZg8gpzXFkE.jpg

But when I try to do the same for my real project - created EF model is very strange

http://magicscreenshot.blob.core.windows.net/screenshots/WfKgc8vBDU8.jpg

as we can see, it created a new entity "SectionSite" with relationship to Site and no relationship to Section. Why it happened and how to fix it?

Upvotes: 1

Views: 42

Answers (2)

Joey Bob
Joey Bob

Reputation: 332

I was able to replicate this if you added in your Section and SectionSite table independently of adding your Site table. This creates the link the following link.

Broken EDMX Model

To resolve this missmatch of identity you can either.

  1. Remove and re-add Site, Section, and SectionSite all at the same time.

OR

  1. Right click "Add a new association" between Site and SectionSite and un-check "Add foreign key properties to the 'Section' entity."

Add Association

  1. Then you need to select the created relationship, properties -> Referential Constraint and update to meet your FK relationship.

Modify Referential Constraint

  1. Once your happy, right click and validate your model and it should now work?

Upvotes: 1

Jelle Oosterbosch
Jelle Oosterbosch

Reputation: 1742

With Entity Framework you don't have to create a mapping table.

If you let your Section refer to multiple Sites (ICollection) and let your Site refer to multiple Sections (ICollection), Entity Framework will create a SectionSite or SiteSection table for you.

Upvotes: 0

Related Questions