Home > ASP.NET, Entity Framework > Define Composite Key in Model.(Core, EF Code First)

Define Composite Key in Model.(Core, EF Code First)


When there are composite keys in database table and when you try to put data annotation it will throw below error

Entity type ‘Orgmember’ has composite primary key defined with data annotations. To set composite primary key, use fluent API

The above code will not work and throw an error

Here is the solution for this problem

Step 1: Modify your modal class like

Step 2: Use the fluent API to set up a composite key.

Hope it helps!

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: