问题描述
我有这段代码,应该使用(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不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!