In EF4.1+, do you have to expose a foreign key property on a dependent entity to enforce an identifying relationship?

StackOverflow https://stackoverflow.com//questions/9571864

Question

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).

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top