EF Code First 给我错误当 IDENTITY

编程入门 行业动态 更新时间:2024-10-23 08:25:16
本文介绍了EF Code First 给我错误当 IDENTITY_INSERT 设置为 OFF 时,无法为表 'People' 中的标识列插入显式值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在试用 Entity Framework 4 的 Code First (EF CodeFirst 0.8),但遇到了一个简单模型的问题,该模型在 Person 0..1 关系code> 和 Profile.以下是它们的定义:

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:

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; } } 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:

public class BodyDB : DbContext { public DbSet<Person> People { get; set; } }

我没有为 Profile 定义 DbSet 因为我认为 People 是它的聚合根.当我尝试添加一个新的 Person - 即使一个没有 Profile 的代码:

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; }

我收到以下错误:

当 IDENTITY_INSERT 设置为 OFF 时,无法为表 'People' 中的标识列插入显式值.

Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF.

当我调用 People.Add() 时,newPerson 对象的 PersonId 属性有一个 0.数据库表是People 和Profiles.PersonId 是People 的PK,是一个自增的Identity.ProfileId 是 Profiles 的 PK,是一个自动增强的 Identity.PersonId 是 Profiles 的非空 int 列.

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.

我做错了什么?我想我遵守了 EF Code First 的所有约定,而不是配置规则.

What am I doing wrong? I think I'm adhering to all the EF Code First's convention over configuration rules.

推荐答案

我收到以下错误:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 'People' 中的标识列插入显式值.

I get the following error: Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF.

我认为 IDENTITY_INSERT 是关闭的自动增量功能.所以,检查数据库中的字段PersonId,看看它是否是一个身份.

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 Code First 给我错误当 IDENTITY

本文发布于:2023-11-16 01:35:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599800.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:给我   错误   EF   Code   IDENTITY

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!