Home > DeveloperSection > Forums > Scaffolding error in asp.net mvc?
Elena Glibart
Elena Glibart

Total Post:32

Points:226
Posted on    November-10-2014 2:36 AM

 ASP.NET MVC ASP.Net  Controller 
Ratings:


 1 Reply(s)
 1585  View(s)
Rate this:
I'm going in circles with either an in valid context type or undefined entity key. I have attached a copy of the longer error message. Just trying to create a test application that is connected to an existing db.



Here is the code - thanks in advance

    public class TestPerson

{

    int ID { get; set; }

    string FirstName { get; set; }

    string LastName { get; set; }

    string Hair { get; set; }

    string Race { get; set; }

    string Measurements { get; set; }

}

public class TestPersonContext : DbContext

{

    public DbSet<TestPerson> TestPersons { get; set; }

}

 



ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    November-10-2014 5:25 AM

You should set one of your properties as the primary key. In your case this will be the ID property:

public class TestPerson

{

    [Key]

    public int ID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Hair { get; set; }

    public string Race { get; set; }

    public string Measurements { get; set; }

}

public class TestPersonContext : DbContext

{

    public DbSet<TestPerson> TestPersons { get; set; }

}

Btw why are all your properties private? It would be recommended to make all them publicly available, especially the primary key.

What is more there is something called Convention over Configuration which is used quite intensively in Entity Framework. One of the conventions is the Primary Key Convention which states:


Don't want to miss updates? Please click the below button!

Follow MindStick