小编Vam*_*msi的帖子

无法将自定义值插入标识列 - 实体框架

我试图关闭身份以插入我自己的值,我遵循的步骤

  1. 将属性StoredGeneratedPattern值更改None为标识列
  2. 通过以xml格式打开,将属性StoredGeneratedPattern值更改为NoneEDMX文件

尝试使用以下代码

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
   int k = Context.ExecuteStoreCommand("SET IDENTITY_INSERT dbo.client ON");
   Context.ClientInfoes.Add(testclient);
   result = Context.SaveChanges();
   int j = Context.ExecuteStoreCommand("SET IDENTITY_INSERT dbo.client OFF");
   scope.Complete();
}
Run Code Online (Sandbox Code Playgroud)

但我仍然收到错误

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

我错过了什么吗?还有其他选择吗?

c# identity entity-framework entity-framework-4 entity-framework-4.1

5
推荐指数
1
解决办法
1858
查看次数