开发者

Problem in mapping fragments in EF4

开发者 https://www.devze.com 2023-01-22 22:49 出处:网络
I have the following model: But when i compile with VS2010, i get the following erro开发者_StackOverflowr:

I have the following model:

Problem in mapping fragments in EF4

But when i compile with VS2010, i get the following erro开发者_StackOverflowr:

Error 2 Error 3007: Problem in mapping fragments starting at lines 1784, 2018:Column(s) [createdby] are being mapped in both fragments to different conceptual side properties.

What do i want ? Actually i want the Note entity to have FK to User entity. When does the error appear ? When i add the FK User to Note i have the error. If i remove the link, no problem. What is the problem ? Thanks John


I also have a "User" entity (based on a View) and several tables with CreatedByUser and ModifiedByUser references.

This works for me without getting the "must be null" error:

  • On the "Note" entity: right-click: “Add > Association...”.
  • Select "User" as the right-side "End Entity".
  • Swap the multiplicities: "Notes" should be "Many" and "User" should be "One".
  • I deselect the right-side Navigation Property, as I'll never navigate from "User" to the other tables: but that's up to you.
  • If you have both "CreatedBy" and "ModifiedBy" properties, then you might want to change the Association Name to something more meaningful (e.g. change "NoteUser" to "NoteCreatedByUser"
  • Ditto for the Navigation Property (e.g. change "User" to "CreatedByUser")
  • Leave “Add foreign key properties....” unchecked.
  • Click OK.
  • IMPORTANT: in the “Properties” window, go to Referential Constraint and click the ellipsis.
  • Select "User" as the Principal.
  • Change the Dependent Property to the appropriate value (e.g. "CreatedBy")
  • Click OK.

Done!

Screen shots from my project below.

Problem in mapping fragments in EF4


My guess is that the association on the User entity has been defined on Username and Username is a candidate key (has a unique index defined on it) in SQL Server. While you can create a relationship based on this setup in SQL Server, current version of EF does NOT support creating associations based on candidate keys: In EF (3.5 and 4.0) FKs MUST point to Primary Keys.

The only way to make this working is to create a new int field inside Note table (like UserId) and then create a new relationship between User.Id (PK) and Note.UserID (FK) and EF will happily create a association based on that for you.

0

精彩评论

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