How to Bulk Update records in Entity Framework?

前端 未结 7 2088
遥遥无期
遥遥无期 2020-12-01 08:00

I am trying to bulk update records using Entity Framework. I have tried Entity Framework.Extensions Update method.

The Update method is abl

相关标签:
7条回答
  • 2020-12-01 08:32

    a) EFCore.BulkExtensions - BatchUpdateAsync

    _dbContext.Set<MyObjectEntity>().BatchUpdateAsync( x => new MyObjectEntity{ Id=123, Quantity=100 });
    

    https://github.com/borisdj/EFCore.BulkExtensions

    "EntityFrameworkCore extensions: Bulk operations (Insert, Update, Delete, Read, Upsert, Sync) and Batch (Delete, Update). Library is Lightweight and very Efficient, having all mostly used CRUD operation. Was selected in top 20 EF Core Extensions recommended by Microsoft."

    b) Or EF Extensions - UpdateFromQuery

    _dbContext.Set<MyObjectEntity>().UpdateFromQuery( x => new MyObjectEntity{ Id=123, Quantity=100 });
    

    Resource:

    https://entityframework-extensions.net/update-from-query

    https://stackoverflow.com/a/63460251/12425844

    Why UpdateFromQuery is faster than SaveChanges, BulkSaveChanges, and BulkUpdate?

    UpdateFromQuery executes a statement directly in SQL such as UPDATE [TableName] SET [SetColumnsAndValues] WHERE [Key].

    Other operations normally require one or multiple database round-trips which makes the performance slower.

    0 讨论(0)
  • 2020-12-01 08:35

    Use ExecuteSqlCommand:

    using (yourDbEntities db = new yourDbEntities())
    {
        db.Database.ExecuteSqlCommand("UPDATE YourTABLE SET Quantity = {0} WHERE Id = {1}", quantity, id);
    }
    

    Or ExecuteStoreCommand:

    yourDbContext.ExecuteStoreCommand("UPDATE YourTABLE SET Quantity = {0} WHERE Id = {1}", quantity, id);
    
    0 讨论(0)
  • 2020-12-01 08:36

    If you don't want to use an SQL statement, you can use the Attach method in order to update an entity without having to load it first :

    using (myDbEntities db = new myDbEntities())
    {
        try
        {
          //disable detection of changes to improve performance
          db.Configuration.AutoDetectChangesEnabled = false;
    
          //for all the entities to update...
          MyObjectEntity entityToUpdate = new MyObjectEntity() {Id=123, Quantity=100};
          db.MyObjectEntity.Attach(entityToUpdate);
    
          //then perform the update
          db.SaveChanges();
        }
        finally
        {
          //re-enable detection of changes
          db.Configuration.AutoDetectChangesEnabled = true;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:41

    I found an easy way to do that without any 3rd party packages:
    By adding one generic extension method SetValue you can simply write:

    Example:

    void Main()
    {
        
        var dc = this; // context
        var p = dc.Purchases.Where(x=>x.Description.ToLower()=="bike")
                            .SetValue(w => w.Description = "Bicycle");
        p.Dump();
        dc.SubmitChanges();
    }
    

    As you can see, any value matching the Where condition can be set explicitly to a new value, so here Bike will be replaced by Bicycle. You can query the table afterwards to see the changes really persisted.

    Of course, you could also omit the Where statement, if you want to change all records like:

    dc.Records.SetValue(x => x.Quantity = 100);
    dc.SubmitChanges();
    

    Entity framework (EF) / LINQ tracks those changes and when you call .SubmitChanges() - as you can see in the SQL tab if you're using LinqPad - it will create SQL code as follows:

    -- Region Parameters
    DECLARE @p0 Int = 3
    DECLARE @p1 VarChar(1000) = 'Bicycle'
    -- EndRegion
    UPDATE [Purchase]
    SET [Description] = @p1
    WHERE [ID] = @p0
    

    For small changes, this is ok, but for large tables it is becoming inefficient, because it uses the ID column to identify and change a record, and not the Description column as defined by .SetValue.

    Theoretically EF could optimize this, but as you can see, it doesn't do it. So if you want true bulk operations you need to run a SQL command instead or create a stored procedure (for complex queries) which you're calling via EF.


    Extension method SetValue

    This extension method does the trick (no other 3rd party packages required):

    // see: https://visualstudiomagazine.com/articles/2019/07/01/updating-linq.aspx
    public static class Extensions
    {
        public static IEnumerable<T> SetValue<T>(this IEnumerable<T> items, 
                                                      Action<T> updateMethod)
        {
            foreach (T item in items)
            {
                updateMethod(item);
            }
            return items;
        }
    }
    

    Note: The example above uses the Nutshell example database, which you can easily create by following this link and the code is written for LinqPad 6 but can be adapted easily (LinqPad 6 uses .NET Core, but you can try it with LinqPad 5 as well for the .NET Framework).

    0 讨论(0)
  • 2020-12-01 08:42

    Use this way if you just want to modify few properties:

    foreach (var vSelectedDok in doks)
    {
        //disable detection of changes to improve performance
        vDal.Configuration.AutoDetectChangesEnabled = false;
        
        vDal.Dokumente.Attach(vSelectedDok);
    
        vDal.Entry(vSelectedDok).Property(x=>x.Status).IsModified=true;
        vDal.Entry(vSelectedDok).Property(x => x.LastDateChanged).IsModified = true;
    }
    vDal.SaveChanges();
    
    0 讨论(0)
  • 2020-12-01 08:47

    Bulk Update can be done in three steps with simple EF instead of separate extension methods :-

    • Load all the entities first.
    • Foreach on each entity and change its field values.
    • After Foreach save the context changes once.

    This will send multiple Update queries in single batch.

    0 讨论(0)
提交回复
热议问题