EF6 database first map one to one relationship

edmx ef-database-first entity-framework-6

Question

In my database I have one-to-one relationships enforced by making a unique constraint on the foreign key table. This is correctly identified by the Sql Server Management Studio as one-to-one relation:

enter image description here

My problem is that EF6 does not identifies this as a one-to-one relation and generates a Collection on the Person class:

enter image description here

I could easily modify the T4 templates to not generate the collection when there is a unique key on the table IF the itemCollection in the t4 templates would contain the unique key information. But they only contain the foreign key information. I guess this is because the edmgen tool does not extract these information from the db.

Is there a way to force the tool to include all the constraints in the database so I can use this information in the t4 templates?

1
4
5/9/2016 2:13:42 PM

Popular Answer

I think the best solution here is to rethink your tables and relationships.

Option 1: If this is a 1 to 1 relationship, can you put everything in one table?

Option 2: Take out the PersonId column. If this is truly a 1 to 1, or even a "1 to 0 or 1" relationship, the tables should share a primary key. The relationship should be between Id and Id. If you do this, EF6 will properly recognize it.

1
7/28/2017 3:43:21 PM


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