Unable to determine key for model using mvc3

I am trying to set fields in a database using the mvc3 method. When I run the program, I get

System.Data.Edm.EdmEntityType :: EntityType "CarModel" does not have a key. Define a key for this EntityType.

my model looks like

public class CarModel { public string VIN { get; set; } public string Make { get; set; } public string Model { get; set; } public string Year { get; set; } public string Color { get; set; } public string Mileage { get; set; } public string Description { get; set; } } 

I saw where people add an identifier, but the database does not have an ID property. And when I try to add [Key] over VIN, which is the primary key in the database. This gives a red squiggly error under the key.

They seem to be missing a link.

+6
source share
2 answers

Invalid reference, probably System.ComponentModel.DataAnnotations ; this is not added for you if you type Ctrl-. with the cursor next to [Key] ?

+9
source

The Entity structure requires each object to define a Primary Key. If you just want to remove the error, add the following:

 [Key] public int id { get; set; } 

and

 public CarModel() { id = 1; } 
+3
source

Source: https://habr.com/ru/post/886509/


All Articles