Error with Entity Framework Associations Because the attributes of the Dependent Role are not the most important ones.

c# dns domain-driven-design entity entity-framework

Question

Using Entity Framework 4.1

I have the following tables I'm using with Entity Framework

  • User ( UserId PK identity, UserName, Password )

  • Physician ( PhysicianId unique identity, UserId refereces User PK, PhysicianName )

  • PhysicianSite ( SiteId, Location, PhysicianId FK to Physician table)

Problem. Entity Framework would not allow me to associate Physician with PhysicianSite because PhysicianId is not the primary key on the Physician table.

User is the base of Physician because my application has other types Users, such as Patients.

I get the following error

Error 2 Error 113: Multiplicity is not valid in Role 'Physician' in relationship 'PhysicianSitePhysician'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.

Could someone tell me how I can create the association with Physician to PhysicianSite in Entity Framework without PhysicianId being the primary key on the Physician table?

1
2
12/9/2011 7:37:32 PM

Accepted Answer

Entity Framework requires foreign keys to link to a table's primary key. However, the primary key to Entity Framework does not have to match the primary key in the database. If you tell EF the PhysicianId is the primary key, it won't go checking if it really is. And it will happily accept foreign keys to the PhysicianId.

If you have some foreign keys to PhysicianId, and others to the real primary key, you're out of luck, but it doesn't seem like you do.

2
12/9/2011 7:52:59 PM

Popular Answer

If you have a link table with a column with identity specification, remove that (it would restrict the link table to only one copy of the ID for that linked table).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow