Entity Framework MVC 3 in C#. Refuses to add values to table 'Person' and People is generated(?)
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.DataAnnotati开发者_高级运维ons;
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 Id
or 'EntityClassName'Id
, it is chosen as the key by default (that's the convention in other words). As you do not have property named Id
or PersonId
- you've got only UserId
, you get first validation error that Person
has got no key. Add the [Key] attribute on UserId
, or use the fluent interface code
//Inside PersonDB class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>().HasKey(x => x.UserId);
}
PluralizingEntitySetNameConvention 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
//Inside PersonDB class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().HasKey(x => x.UserId);
....
modelBuilder.Conventions.Remove<ConventionTypeNameFromTheLinkAbove>();
}
If you look carefull, you can see that KeyAttributeConvention is also one of default builtin conventions.
"People" is the name that was automatically assigned to the EntitySet when your model was generated. EntityFramework tries to do some slick automatic pluralization of table/entity names so it decided a row in the Person table was a "Person" and the entire table itself should be referred to by a repository named "People."
You need to do db.People.Add(p);
You also need to revisit the Person table in your database, because EntityFramework wasn't able to easily determine the keys defined in that table (which EntityFramework needs for maintaining state of your repository/table when changes are being made to its internal ObjectStateManager etc).
精彩评论