【发布时间】:2012-09-25 17:15:39
【问题描述】:
我正在尝试关闭身份以插入我自己的值,我遵循的步骤
- 将标识列的属性
StoredGeneratedPattern值更改为None - 将EDMX文件中的
StoredGeneratedPattern属性值更改为None,以xml格式打开
尝试使用以下代码
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();
}
但我仍然收到错误消息
无法为表中的标识列插入显式值 IDENTITY_INSERT 设置为关闭
我错过了什么吗?还有其他选择吗?
【问题讨论】:
-
您的上下文是 ClientInfoes,但您的 SQL 是 dbo.client。 ClientInfoes 是否映射到客户端?还是客户信息表?
-
clientinfoes映射到client,client是sql中的表。没有身份,插入工作正常,但我的要求是传递自定义客户端 ID。
标签: c# entity-framework entity-framework-4 entity-framework-4.1 identity