Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique Keys not recognized by Entity Framework

I have two tables, Reports and Visualizations. Reports has a field, VisualizationID, which points to Visualization's field of the same name via a foreign key. It also has a unique key declared on the field. VisualizationID is not nullable. This means the relationship has to be 0..1 to 1, because every Reports record must have a unique, not null Visualizations record associated with it.

The Entity Framework doesn't see it this way. I'm getting the following error:

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

What's the problem here? How can I make the EF recognize the proper relationship multiplicity?

like image 890
David Pfeffer Avatar asked Jan 26 '10 17:01

David Pfeffer


2 Answers

The EF is complaining because it sounds like you are using an FK Association - which means that the VisualizationID is a property of the Entity and there is a Visualization reference too - and you can't do this with FK Associations.

If however you use Independent Associations - which means there is no VisualizationID property - you can narrow the cardinality.

So the solution is to remove the VisualizationID property from the Entity, at which point you need to go ahead an map the association.

Hope this helps

Alex

like image 187
Alex James Avatar answered Nov 05 '22 16:11

Alex James


I've just stumbled upon the very same problem - Alex, your explanation is correct in my case , but of course by removing the FK column from the conceptual model, I no longer have the ability to later on switch my related object by changing the value of the FK field...I'll have to revert to the old tricks to do this!

Is there any plans to allow us to have our cake and eat it too with 1 to 0..1 relationships, i.e. be able to have the FK field as well?

Greg

(p.s. I would have commented, but my rep isn't high enough yet!)

like image 43
Greg Neilson Avatar answered Nov 05 '22 15:11

Greg Neilson