EF代码首先给我错误当IDENTITY_INSERT设置为OFF时,无法在表'People'中插入标识列的显式值 [英] EF Code First giving me error Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF
问题描述
using System;
命名空间BB.Model.Implementations {
public class Person {
public int PersonId {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public DateTime? DOB {get;组; }
public virtual Profile Profile {get;组;
}
和可选人的个人资料:
命名空间BB.Model.Implementations {
public class Profile {
public int ProfileId {get;组; }
public int PersonId {get;组; }
public string DisplayName {get;组; }
public virtual Person Person {get;组; }
}
数据库上下文如下所示:
使用System.Data.Entity;
使用BB.Model.Implementations;
命名空间BB.Services.Implementations {
public class BodyDB:DbContext {
public DbSet< Person>人{get;组;
}
我没有为配置文件定义DbSet,因为我以人为本,当我尝试添加一个新的人 - 即使没有一个配置文件与这个代码:
public Person Add(Person newPerson) {
Person person = _bodyBookEntities.People.Add(newPerson);
_bodyBookEntities.SaveChanges();
退货人;
}
我收到以下错误:
当IDENTITY_INSERT设置为OFF时,无法在表'People'中为identity列插入显式值。
当我调用People.Add时,newPerson对象的PersonId属性为0。数据库表是人员和配置文件。 PersonId是人的PK,是一个自动增量身份。 ProfileId是个人资料的PK,是一种自动识别身份。 PersonId是个人档案的非空int列。
我做错了什么?我认为我遵守所有EF Code First的配置规则约定。
I得到以下错误:
当IDENTITY_INSERT设置为OFF时,表'People'中的标识列无法插入显式值。
我认为IDENTITY_INSERT是关闭的自动增量功能。
所以,检查数据库中的PersonId字段,看它是否是一个标识。
此外,也许这也解决了你的问题。
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PersonId {get;组; }
I'm trying out Entity Framework 4's Code First (EF CodeFirst 0.8) and am running into a problem with a simple model that has a 1 <--> 0..1 relationship -- between Person and Profile. Here's how they're defined:
using System;
namespace BB.Model.Implementations {
public class Person {
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DOB { get; set; }
public virtual Profile Profile { get; set; }
}
and the optional person's profile:
namespace BB.Model.Implementations {
public class Profile {
public int ProfileId { get; set; }
public int PersonId { get; set; }
public string DisplayName { get; set; }
public virtual Person Person { get; set; }
}
The DB context looks like this:
using System.Data.Entity;
using BB.Model.Implementations;
namespace BB.Services.Implementations {
public class BodyDB : DbContext {
public DbSet<Person> People { get; set; }
}
I didn't define a DbSet for Profile because I consider People to be its aggregate root. When I try to add a new person -- even one without a profile with this code:
public Person Add(Person newPerson) {
Person person = _bodyBookEntities.People.Add(newPerson);
_bodyBookEntities.SaveChanges();
return person;
}
I get the following error:
Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF.
The newPerson object has a 0 for the PersonId property when I call People.Add. The database tables are People and Profiles. PersonId is the PK of People and is an auto-increment identity. ProfileId is the PK of Profiles and is an auto-incement identity. PersonId is a non-null int column of Profiles.
What am I doing wrong? I think I'm adhering to all the EF Code First's convention over configuration rules.
I get the following error: Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF.
I think that the IDENTITY_INSERT is the Auto Increment functionality which is off. So, check the field PersonId in the database to see if it is an identity.
Besides, maybe this will fix your problem too.
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PersonId { get; set; }
这篇关于EF代码首先给我错误当IDENTITY_INSERT设置为OFF时,无法在表'People'中插入标识列的显式值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!