Liza
Liza

Reputation: 43

Entity Framework: Created a entity and insert data in table without primary key

I am using .NET core 6. I want t insert data in SQL server without a PK. I have used "KeyLess" and "HasNoKey()". But when i am trying to insert data into DB it is showing me error. Can anyone please tell how to insert without PK?

Error:

"unable to track an instance of type 'entityModel' because it does not have a primary key. only entity types with a primary key may be tracked.'"

Upvotes: 2

Views: 2947

Answers (1)

justdengjian
justdengjian

Reputation: 51

Always the entity has a primary key or a compound primary key.

Use compound primary key like this:

modelBuilder.Entity<UserRole>().HasKey(x => new { x.UserId, x.RoleId });

Upvotes: 5

Related Questions