One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'Person' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: The EntitySet People is based on type Person that has no keys defined.
—> Person.cs ( in models )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Portal.Models
{
public class Person
{
[Required]
public int UserId;
[Required]
public string FirstName;
[Required]
public string LastName;
}
}
— > PersonDB.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Portal.Models
{
public class PersonDB : DbContext
{
public DbSet<Person> Person { get; set; }
}
}
— > web.config connectionstring.
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|ASPNETDB.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="PersonDB"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|ASPNETDB.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
— > AccountController ( trying to add values if account creation succeded )
PersonDB db = new PersonDB();
Person p = new Person {UserId = 1, FirstName = "hej", LastName = "padig"};
db.Person.Add(p);
db.SaveChanges();
Here I’m just trying to add some test values to the table, the table consists of UserId with is int, and nvarchar FirstName, LastName.
Where does this People come from in the validation error?
“The EntitySet People is based on type Person” << This is driving me insane.
Don’t get this, I’ve spent way too much time with this which essentially just is an insert into query…
First, every entity type needs a key. If you got the property named
Idor'EntityClassName'Id, it is chosen as the key by default (that’s the convention in other words). As you do not have property namedIdorPersonId– you’ve got onlyUserId, you get first validation error thatPersonhas got no key. Add the [Key] attribute onUserId, or use the fluent interface codePluralizingEntitySetNameConvention is one more example of conventions. Entity Framework uses this convention by default and makes entity set names plural. If you do not wish to use this convention, you can remove it. Here is the list of all the default conventions. You can remove undesired convention by calling
If you look carefull, you can see that KeyAttributeConvention is also one of default builtin conventions.