SCAFFOLDING ERROR IN ASP.NET MVC?

Elena Glibart

Total Post:42

Points:296
Posted by  Elena Glibart
 2257  View(s)
Ratings:
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; }

}

 

  1. ezra heywood

    Post:145

    Points:1019
    Re: Scaffolding error in asp.net mvc?

    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:

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!