Entity Framework Database First - Composite Foreign Keys
composite-key, ef-database-first, entity-framework, entity-framework-5
Solution
Entity Framework Database First doesn't behave with unique keys as you need. If you want them to be navigation properties, you should set them as foreign keys.
Now, you have to choice: 1) you just need NavPrs, 2) You also need the junction table as an entity.
1) To achieve this, just set those foreign keys as a composite primary key.
2) To achive this, leave them as foreign keys and declare an identity type ID column for your junction table and set it as the primary key.
This should work for you
Problem
I have a database that contains a couple of composite foreign keys. For example, here is the generation script for the foreign key: ``` ALTER TABLE [dbo].[WorkingRosters] WITH NOCHECK ADD CONSTRAINT [FK_WorkingRoster_ShiftLeaveCode] FOREIGN KEY([OrganizationID], [ShiftLeaveCode]) REFERENCES [dbo].[ShiftLeaveCodes] ([OrganizationID], [Code]) GO ``` I am attempting to use Entity Framework 5 Database-First to generate a model from this database. However, the associations for the composite foreign keys are not being generated with all the tables and other simple foreign keys. How can I either: - manually create these composite foreign keys in the xml behind the edmx (painful) - have entity framework properly generate these foreign keys so that I have have the mappings Thanks!