本文介绍了实体框架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();
}

我收到此错误。


解决方案

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

解决方案

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不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:27