EF 批量增加問題

weixin_33728268發表於2017-05-21
            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();
            }
            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();
            }

相關文章