1

If I want to create an identifying relationship using EF 4.1+ (or EF5 Beta), does this mean I have to expose a foreign key property on the dependent entity? When using the Fluent API? And when the DbContext is in a different project than the Entity?

I can't see any other way to do it. The DbModelBuilder needs to know the foreign key property in order to include it as part of the primary key. However if the property is not exposed, and the Entity & DbContext implementations are in different projects/libraries, there is no way to expose it (don't want to put InternalsVisibleTo on the entity lib).

danludwig
  • 46,965
  • 25
  • 159
  • 237
  • 1
    What is an "identifying" relationship? Can you add a small example to your question? – Slauma Mar 05 '12 at 18:53
  • @Slauma: [Here](http://stackoverflow.com/questions/4922228/entity-framework-4-delete-object-from-entity-collection/4925040#4925040) you have example. – Ladislav Mrnka Mar 05 '12 at 19:41
  • @Slauma, I was also referring to the msdn doc "Considerations for Identifying and Non-identifying Relationships" found here: http://msdn.microsoft.com/en-us/library/ee373856.aspx – danludwig Mar 05 '12 at 19:47
  • @LadislavMrnka and danludwig: Thanks, never heard of it. Then I even have such an "identifying relationship" in one of my models. (But I am deleting stupidly, never thought that I just could clear the collection in that case.) – Slauma Mar 05 '12 at 20:45

1 Answers1

4

Identifying relationship always needs FK property exposed on the dependent entity because the property must be part of primary key - that is what makes it identifying. I didn't try it with DbContext but unless there is some internal issue / bug it should work.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks Ladislav, yes it works when exposing the fk property on the dependent entity. I was hoping there would be some magic way to enforce the identifying relationship without having to expose the foreign key (i.e. expose only the navigation property). – danludwig Mar 05 '12 at 19:46