GenesysStack.com

Did you know how to use EF ID functions even with Composite Primary Keys?

Did you know how to use EF ID functions even with Composite Primary Keys?

Composite primary keys, or a primary key made of multiple columns, make code reuse difficult. An abstract data access class such as SaveableEntity and ReadOnlyEntity can have an ID property, and always know that the ID field that is guaranteed to be unique.

Alternatively, composite keys are usually different for each table…so the same base class needs to change its primary key setup for each table.

A simple solution for transactional systems is to select your data from a view that concatenates the composite keys into a single ID column by using the Concat() function.

SelectConcat(Field1, Field2)As ID, Field1, Field2 From MySchema.MyTable

Leave your comment