【发布时间】:2013-10-11 05:52:04
【问题描述】:
我正在尝试使用 dbContext 将实体保存到数据库。
Type entityType = Type.GetType("class");
object ob = db.Set(entityType).Create(entityType);
ob.GetMethod("set_Id").Invoke(ob, new object[] { newId });
//...other set code...
db.Set(entityType).Add(ob);
db.SaveChanges(); -- here fires exception
但是在 SaveChanges 触发异常之后
"无法在表 'TableName' 中插入标识列的显式值 当 IDENTITY_INSERT 设置为 OFF”时。
在分析器中,我看到带有我设置的 id 的标准插入批次。如何在身份插入 ON 的情况下将实体对象添加到 db,或者如何只保存新的 entityObject?
【问题讨论】:
标签: c# entity-framework dbcontext