Create association on non-primary key fields with Entity Framework 4.1 Fluent API

It is not possible. Relations in EF follows exactly same rules as in the database. It means that principal table must have unique identifier which is referenced by dependent table. In case of database the identifier can be either primary key or unique column(s) of principal table. Otherwise it is not valid relation.

Entity framework doesn’t support unique keys. If you want to have one-to-many relation between Report and RunStat your dependent table (RunStat) must contains column with value of Report.ReportKey. There is no other way to make it automatic – otherwise you must simply make it custom property and fill it from entity framework manually when you need it.

Leave a Comment