这样的代码:
using (TransactionScope ts = new TransactionScope())
{
DBContext context = new DBContext();
SupEntity sup = new SupEntity();
context.Set<SupEntity>().Add(sup);
context.SaveChanges();
CusEntity cus = new CusEntity();
cus.Sup_ID = sup.Sup_ID; // sup.Sup_ID is autoincrement
context.Set<CusEntity>().Add(cus);
context.SaveChanges(); //throw exception in this line:database is locked\r\ndatabase is locked
ts.Complete();
}
似乎第二次调用SaveChanges方法时,上下文尝试与数据文件建立新连接,但数据文件已打开。 我怎样才能两次调用SaveChanges?