实体框架“无法为标识列插入显式值"错误

编程入门 行业动态 更新时间:2024-10-25 00:26:35
本文介绍了实体框架“无法为标识列插入显式值"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我了解错误消息在说什么.我只是不确定为什么要这么说.我的表具有一个Identity,并将其设置为自动递增,但是当我去保存一个实体时,出现以下错误:

I understand what the error message is saying. I'm just not sure why it is saying it. My table has an Identity and it is set to auto-increment, yet when I go to save an entity I get the following error:

当IDENTITY_INSERT设置为OFF时,无法在表会议"中为标识列插入显式值.

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

我使用的是代码优先的方法,我的 Conference 实体看起来像这样:

I'm using a code-first approach and my Conference entity looks like this:

public class Conference { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int DugoutId { get; set; } public int ConferenceDetailsId { get; set; } [ForeignKey("Id")] public virtual Dugout Dugout { get; set; } [ForeignKey("Id")] public virtual ConferenceDetail ConferenceDetail { get; set; } }

当我要保存会议时,值如下:

When I am going to save the Conference, the values are as follows:

Id: 0 DugoutId: 15 ConferenceDetailsId: 1 Dugout: null ConferenceDetail: null

任何想法为何无法保存?

Any ideas why this won't save?

推荐答案

[ForeignKey]服装指向Dugout和ConferenceDetails的错误属性.应该是ForeignKey("DugoutId")和ForeignKey("ConferenceDetailsId").

The [ForeignKey] attibutes are pointing to wrong property for Dugout and ConferenceDetails. Should be ForeignKey("DugoutId") and ForeignKey("ConferenceDetailsId").

更多推荐

实体框架“无法为标识列插入显式值"错误

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

发布评论

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

>www.elefans.com

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