首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

EF 批量批加有关问题

2012-05-20 
EF 批量批加问题using (TransactionScope scope new TransactionScope()){using (var objContext GetC

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();


}


这里加一句,这东西不实际入库是是无法获取滴,反正你前面是事务保证,也不怕错误

热点排行