Entity Framework 4.0 composite database keys

composite-key entity-framework primary-key

Question

Composite primary keys are heavily used in our company's redesign of a large database.

Ignoring performance effects, would this present any challenges when using this database with Entity Framework 4.0? Although it's doubtful that the database structure will change and I'm not asking for "philosophical" argument, what are the practical repercussions?

"Composite key make any form of Object/Relational mapping and persistance in general difficult," claims Jonathan Miller. but he never fully explains why. Is this relevant to the key handling in Entity Framework 4.0?

1
12
12/18/2009 4:19:49 AM

Accepted Answer

No, EF4 works absolutely well with composite keys.

A table containing a composite key and a surrogate key is the source of the issue. Each model can only have one key that can have several fields, but from the perspective of the designer, it can only have one field. I'm unsure about changing XML by hand or just using code for mapping.

If you require a composite and surrogate key on the same table, you may designate a field to be an Identity rather than a key. The Identity (Id) column will increase and be queryable normally despite not being utilized by the ObjectContext or ObjectStateTracker.

13
7/9/2014 1:59:59 PM

Popular Answer

EF4 and composite keys have caused me issues. It does not enable the usage of columns in join tables as parts of multiple keys.

For further information, see my earlier inquiry, mapping composite foreign keys in an Entity Framework many-to-many relationship. The crux of it is that you'll see errors like this when you have a join table (depicting a many-many connection) and both relationships have a common key.

Error 3021: Problem in mapping fragments...: Each of the following columns in table PageView is mapped to multiple conceptual side properties: PageView.Version is mapped to (PageView_Association.View.Version, PageView_Association.Page.Version)

Duplicating the column was the only workaround, which is counterproductive given that it was placed there in the first place.

Good fortune!



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