EF 批量批加问题
using (TransactionScope scope = new TransactionScope())
{
using (var objContext = GetContext())
{
objContext.SMS.Add(model);
foreach (Product_SMS m in list)
{
//m.SmsID = model.ID;这里获取不到ID
objContext.Product_SMS.Add(m);
}
objContext.SaveChanges();
}
scope.Complete();
}
EF 框加。。model 为短信内容,,list为短信与产品相关信息列表,
想。先添加model, 然后遍历list 添加关联信息。
问题是。怎样得到model.id 短信 (为自动编号ID)
------解决方案--------------------
using (TransactionScope scope = new TransactionScope())
{
using (var objContext = GetContext())
{
objContext.SMS.Add(model);
objContext.SaveChanges();
foreach (Product_SMS m in list)
{
//m.SmsID = model.ID;这里获取不到ID
objContext.Product_SMS.Add(m);
}
objContext.SaveChanges();
}
scope.Complete();
}
这里加一句,这东西不实际入库是是无法获取滴,反正你前面是事务保证,也不怕错误