- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试 Entity Framework 4 的 Code First (EF CodeFirst 0.8),但遇到了一个简单模型的问题,该模型在 Person
之间具有 1 <--> 0..1 关系。和Profile
。它们的定义方式如下:
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; }
}
数据库上下文如下所示:
public class BodyDB : DbContext
{
public DbSet<Person> People { get; set; }
}
我没有定义 DbSet
对于 Profile
因为我认为People
为其聚合根。当我尝试添加新的Person
时- 即使没有 Profile
使用此代码:
public Person Add(Person newPerson)
{
Person person = _bodyBookEntities.People.Add(newPerson);
_bodyBookEntities.SaveChanges();
return person;
}
我收到以下错误:
Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF.
newPerson
对象有一个 0
对于PersonId
当我调用People.Add()
时属性(property)。数据库表是People
和Profiles
。 PersonId
是People
的PK并且是一个自增Identity。 ProfileId
是Profiles
的PK并且是一个自动增量的身份。 PersonId
是 Profiles
的非空 int 列.
我做错了什么?我认为我遵守 EF Code First 的所有约定优于配置规则。
最佳答案
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字段,看看它是否是一个身份。
此外,也许这也能解决您的问题。
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PersonId { get; set; }
关于entity-framework - EF Code First 给出错误:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'People' 中插入标识列的显式值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4682504/
我正在 R 中使用 RecordLinkage 库。 我有一个包含 ID、姓名、电话、邮件的数据框 我的代码如下所示: ids = data$id pairs = compare.dedup(data
我目前正在构建一个新的 ASP.NET MVC 5 项目,我想在 9 月左右发布。我需要选择一个成员(member)系统,但我目前对我应该采取哪个方向感到很困惑。当前的 SimpleMembershi
我正在为 Brackets 定制一个大纲插件,它使用正则表达式来识别当前打开的文件的大纲。 我使用 regex101.com 创建了以下正则表达式(使用环视来确定该行以七个空格开头并以“SECTION
我已在表中将一列标记为“身份” create table Identitytest( number int identity(1,001) not null, value varch
我不知道那是字符串还是数组... char str4[100] = { 0 }; 那个代码是字符串? 如果是,它打印什么? 最佳答案 I dont know if that a string or a
我这里有一个场景,当用户想要重置密码时,系统必须通过电子邮件向用户发送一个随机生成的临时密码。我尝试将临时密码存储到数据库中的一个新列中,但我不确定这种方法是否有效。有些人建议使用 token,如下所
Vista 的现代 Windows 应用程序中有一个很好的功能。它是窗口标题中的图片。例如新的 skype (v4) 和 google chrome 都有它。 我在想它背后的技术是什么?如果你关闭 a
比较相同泛型类型的两个实例的最佳(最简洁和最佳)方法是什么,以便比较引用类型的身份(相同的对象,所以不是调用 Equals) 和 value 类型以获得值 equality。 目前我这样做: stat
我使用以下 C# 代码来获取处理器信息。如果我在虚拟机上运行我的应用程序,则管理类为空。我使用 Oracle VM VirtualBox 作为我的虚拟电脑 (Windows XP SP3) Syste
创建帐户后,Windows 帐户(本地、域、Active Directory)的 SID 是否会更改?如果是,在什么条件下。 最佳答案 是的,当您将帐户迁移到新域时,它会发生变化。 这就是您 AD 帐
我正在使用 Identity Server 4 并且我已经自定义了我的 ASP.NET Identity 用户,如下所示: public class ApplicationUser : Identit
我创建了一个 IIS 管理工具,旨在创建新应用程序、将它们分配到新的 AppPool,并为与该 AppPool 关联的身份添加所需的文件夹 ACL。根据this article ,每当创建新的应用程序
我使用 ASP.NET Identity .. 我想将 session 超时设置为无限制或最大值。我试过一些东西,但没有效果。注意:我使用共享主机。 谢谢你。 //web.config /
我有一台 Win 2008 R2 Enterprise 机器,它在几个网站上运行良好,每个网站都有自己的应用程序池。 我在向 IIS AppPool\A、IIS AppPool\B 等授予权限(使用
现有数据库模型(简化): 1 个用户可以加入 1 个或多个访问组。 1个AccessGroup可以有1个或多个AccessItens。 MSDN Says: When an identity is c
在具有单个表继承层次结构的 Hibernate/JPA 环境中使用 PostgreSQL 时,我看到了奇怪的行为。 首先是我的环境: PostgreSQL 8.3 Spring 2.5.6SEC01
是声明“一个类具有唯一标识”。是真是假? Java 中的对象有其唯一标识(至少通过它们的内存地址),但是类也有唯一标识吗?由于类不是对象,我对此感到困惑。或者是否需要实例化一个类(甚至可能)? 最佳答
我正在尝试通过将主要组件分解为单独的网络服务器来使用微服务架构来实现网络应用程序。我正在使用 ASP.NET Identity(仅电子邮件/用户名登录,无 Facebook 等)和“主”应用程序服务器
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How do you like your primary keys? 我知道使用 GUID 的好处,以及使用
我可以这样获取所有用户 var users = UserManager.Users.ToList(); 我能找到这样的角色 var role = db.Roles.SingleOrDefault(m
我是一名优秀的程序员,十分优秀!