简体   繁体   English

实体框架6交易

[英]Entity framework 6 transaction

I need to insert new rows in two of my tables first table's auto generated id field is one of the field of second table .currently I'm using transaction for inserting data. 我需要在两个表中插入新行,第一个表的自动生成的ID字段是第二个表的字段。当前,我正在使用事务插入数据。 My current code is given below 我当前的代码如下

        using (var context = new ApplicationDatabaseEntities())
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                   foreach (var item in itemList)
                   {
                    context.MyFirstEntity.Add(item);
                    context.SaveChanges();

                    mySecondEntity.MyFirstEntityId = item.Id;
                    context.MySecondEntity.Add(mySecondEntity.MyFirstEntityId );
                    context.SaveChanges();

                   }
                  transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Console.WriteLine(ex);

                }
            }
        }

The above code is working fine .my question is, is this the correct way? 上面的代码工作正常。我的问题是,这是正确的方法吗? I mean if I have 1000 or 2000 items for insertion does my current code affect the performance 我的意思是,如果我要插入1000或2000个项目,我当前的代码会不会影响性能

Code can be improved with implicit transaction: 可以使用隐式事务来改进代码:

foreach (var item in itemList)
{
    context.MyFirstEntity.Add(item);
    mySecondEntity.MyFirstEntity = item;
    context.MySecondEntity.Add(mySecondEntity);
}
context.SaveChanges();

Note: instead of id I've used navigation property. 注意:我使用的是导航属性,而不是id。

You should wrap your transaction in a using statement and rollback when an exception is thrown. 您应该将事务包装在using语句中,并在引发异常时回滚。

using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
   try
   {
      foreach (var item in itemList)
      {
         context.MyFirstEntity.Add(item);
         mySecondEntity.MyFirstEntity = item;
         context.MySecondEntity.Add(mySecondEntity);
      }

      transaction.Commit();
   }
   catch (Exception e)
   {
      transaction.Rollback();
   } 
}

More info here . 更多信息在这里

It depends. 这取决于。 If the whole batch has to be transactional then you have no other way as do in one big transaction. 如果整个批次都必须是事务性的,那么您就无法像大笔交易中那样。 If transaction has to be guaranteed for tupples only then if the time of transaction is big enough you may face some locks. 如果仅必须保证交易的价格,则交易时间足够长,您可能会遇到一些麻烦。 Then you can just do transaction in the loop for each tupple. 然后,您可以在循环中为每个tupple进行事务。

Also you can do what you are doing in one go without explicit transaction. 您也可以一口气做自己在做的事情,而无需明确的交易。 You can SaveChanges after the loop and it will be transactional: 您可以在循环后保存SaveChanges ,它将是事务性的:

foreach (var item in itemList)
{
    context.MyFirstEntity.Add(item);
    mySecondEntity.MyFirstEntity = item;
    context.MySecondEntity.Add(mySecondEntity);
}
context.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM