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:
My problem is that EF6 does not identifies this as a one-to-one relation and generates a Collection on the Person class:
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?
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.