实体框架IDENTITY_INSERT ON不起作用 [英] Entity Framework IDENTITY_INSERT ON doesn't work

查看:117
本文介绍了实体框架IDENTITY_INSERT ON不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,应该使用(MCT_DB_ArchiveEntities ent = new MCT_DB_ArchiveEntities( ))
{
ent.ExecuteStoreCommand( SET IDENTITY_INSERT [clicks] ON);
ent.clicks.Attach(ck);
ent.clicks.Context.ObjectStateManager.ChangeObjectState(ck,System.Data.EntityState.Added);
ent.SaveChanges();
}

我收到此错误。


当IDENTITY_INSERT设置为OFF时,无法在表 clicks中为标识列插入显式值。



解决方案

它不起作用。仅当身份插入与真实插入在同一连接上打开时,它才起作用。在您的情况下,可以使用两个不同的连接。要使其正常工作,您必须维护自己的数据库连接并将其传递给ObjectContext的构造函数。


I have this code that is supposed to insert the record with identity insert on

using (MCT_DB_ArchiveEntities ent = new MCT_DB_ArchiveEntities())
{
  ent.ExecuteStoreCommand("SET IDENTITY_INSERT [clicks] ON");
  ent.clicks.Attach(ck);
  ent.clicks.Context.ObjectStateManager.ChangeObjectState(ck, System.Data.EntityState.Added);
  ent.SaveChanges();
}

I get this error.

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

解决方案

It is not supposed to work. It works only if the identity insert is turned on on the same connection as the real insert. In your case two different connections can be used. To make it work you have to maintain your own DB connection and pass it to ObjectContext's constructor.

这篇关于实体框架IDENTITY_INSERT ON不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆