0

I have an linq to entity mapping issue. I have three tables.

Table 1 - ItemsB(ID(key), Part_Number(will be null until built), other item b information)
Table 2 - ItemsA(ID(key), Part_Number(will be null until built), other item a information)
Table 3 - WebItems(Item_id, web item information) *Consists of items from both ItemsB and ItemsA after they are built and pushed over to this table.

ItemsA/ItemsB will have a 1 to 0.1 relationship with WebItems. Part_Number maps to Item_id.

I am using EF4.0.

Problem is after i set up the association/mappings as stated above i get an error message stating: "Problem in mapping fragment at lines so and so: Column [Part_Number] are being mapped in both fragments to different conceptual side properties."

Usually i know what to do in this case. Get rid of the property [Part_Number]. Problem is i need to access [Part_Number] in both ItemsB and ItemsA quite frequently without going to webitems. Not to mention webitems will not always have the [Part_Number] populated at certain points depending upon whether the items have be pushed to webitems.

Does anyone know how to get around this?

Thanks in advance.

Billy Logan
  • 2,713
  • 15
  • 39
  • 48

1 Answers1

0

As I understand it ItemA and ItemB to WebItem in one-to-one relation. One-to-one relation in EF always demands that relation is build on primary keys and one side is mandatory (principal) because dependent entity will have primary key and foreign key in one column. Once you assign primary key in dependent entity you must have principal entity with the same primary key otherwise you will violate referential integrity defined by foreign key.

The problem is that your Part_Number is primary key and foreign key in the same time. To allow such mapping in EFv4 you must use Foreign Key association instead of Independent association. Here is brief description how to create foreign key association in the designer. Once you define referential constrain get back to mapping window of association and delete the mapping.

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • Ladislav, i updated my ItemA and ItemB table a bit to show that the Part_Number is not the primary key in those tables(sorry about that). So as i understand it ItemA/ItemB will have a 1 to 0.1 relation with WebItem. Reason being is that ItemA will not exist in WebItem until it is built, at that point there will be only 1 matching item. The same goes for ItemB. Does this make sense? I will look at your comment to see if it still applies. thanks for your response – Billy Logan May 02 '11 at 19:23
  • It makes probably sense but it is not possible in EF. Any one-to-one relation is based on uniqueness of FK. It means that FK must be either primary key or unique key. The problem is that EF doesn't support unique keys at all and because of that one-to-one relations can be modelled only on top of shared primary keys. – Ladislav Mrnka May 02 '11 at 19:36
  • i see and i kind of thought this would be the answer. Will have to go the SP route in this case. Thank you for your time. – Billy Logan May 02 '11 at 19:38