实体框架 - 在调用'SaveChanges'之前是否有任何方法在事务内部检索新创建的ID(标识)?实体框架 - 在交易中的'SaveChanges'之前检索ID
我需要一个第二个插入的ID,但它始终返回0 ...是由数据库生成
ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
objectContext.Connection.Open();
using (var transaction = objectContext.Connection.BeginTransaction())
{
foreach (tblTest entity in saveItems)
{
this.context.Entry(entity).State = System.Data.EntityState.Added;
this.context.Set<tblTest>().Add(entity);
int testId = entity.TestID;
.... Add another item using testId
}
try
{
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
objectContext.Connection.Close();
throw ex;
}
}
objectContext.Connection.Close();
http://stackoverflow.com/questions/6029711/id-of-newly-added-entity-before-savechanges – Zaki
哦,希望有一种方式,但谢谢。 – user1948635