When a database table has composite keys (multiple columns are in the key) you cannot use the usual key definition
[Key]
[Display(Name = “UserId”)]
public string UserId { get; set; }[Key]
[Display(Name = “RoleId”)]
public string RoleId { get; set; }
When you run the application you get the error message:
{“error”:”Entity type ‘AspNetUserRoles’ has composite primary key defined with data annotations. To set composite primary key, use fluent API.”}
To declare composite keys, in the fluent API format
In the model declare the keys
public class AspNetUserRoles { // Composite primary keys // Initialized in ApplicationDbContext.OnModelCreating() public object UserId { get; internal set; } public object RoleId { get; internal set; } ...
In the ApplicationDbContext file add the lines to the OnModelCreating(ModelBuilder builder) method
// Composite primary key builder.Entity().HasKey(table => new { table.UserId, table.RoleId });