EF code第一:无法插入表'mytable的“标识列显式值时IDENTITY

编程入门 行业动态 更新时间:2024-10-24 10:19:07
本文介绍了EF code第一:无法插入表'mytable的“标识列显式值时IDENTITY_INSERT设置为OFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在设置数据库(SQL Server)的主键标识

在使用EF中插入数据,我得到错误

  

无法插入的标识列显式值'myTable的'  当IDENTITY_INSERT设置为OFF

这个特定的实体,尽管我使用与其他实体同样的方法来插入数据并没有得到错误和他们在一起。

公共类的MyTable    {    [按键/ *,DatabaseGenerated(DatabaseGeneratedOption.Identity)* /]    公众诠释ID {获取;设置;}    公共字符串名称{;设置;}    公共字符串类型{获取;设置;}    公共字符串状态{获取;设置;}    公众诠释国家{获取;设置;}    公开日期时间起始日期{获取;设置;}    公众的DateTime DUE_DATE {获取;设置;}    }`

查询

MyTable的检查=新MyTable的();            check.name =检查名;            check.type =DB;            check.status =新;            check.country =国家;            check.start_date = DateTime.Now;            check.due_date =的dueDate;            db.myTables.Add(检查);            db.SaveChanges();

更新

注释上面行后

(DatabaseGenerated(DatabaseGeneratedOption.Identity))

它抛出一个异常

  

在ReferentialConstraint从属属性映射到一个  存储生成列。专栏:ID

解决方案

我不知道这是否是你的情况太....但第二个错误says..you使用的是主键在一些外键这关系仅适用于有DatabaseGeneratedOption.None属性。

因此​​,要使用你的主键标识可以用DatabaseGeneratedOption.Identity装饰它在这种情况下,它会抛出一个异常,因为你不能用身份关系(使用DatabaseGeneratedOption.None代替)。

Set the primary key to identity in database (SQL Server)

When using EF to insert data I am getting error

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

with this particular entity, though I am using the same approach with other entities to insert the data and getting no error with them.

public class MyTable { [Key/*,DatabaseGenerated(DatabaseGeneratedOption.Identity)*/] public int ID{get;set;} public string name {get;set;} public string type {get;set;} public string status {get;set;} public int country {get;set;} public DateTime start_date {get;set;} public DateTime due_date {get;set;} }`

Query

MyTable check= new MyTable(); check.name = checkName; check.type = "DB"; check.status = "New"; check.country = country; check.start_date = DateTime.Now; check.due_date = dueDate; db.myTables.Add(check); db.SaveChanges();

Updated :

After uncommenting the above line

(DatabaseGenerated(DatabaseGeneratedOption.Identity))

it's throwing an exception

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'

解决方案

I am not sure if it is in your case too....but the second error says..you are using your primary key as foreign key in some relation which is only valid for property having DatabaseGeneratedOption.None .

So to use your primary key as identity you can decorate it with DatabaseGeneratedOption.Identity and in that case it will throw an exception because you can not use identity for relation(use DatabaseGeneratedOption.None instead).

更多推荐

EF code第一:无法插入表'mytable的“标识列显式值时IDENTITY

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

发布评论

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

>www.elefans.com

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