简体   繁体   中英

EF Core Update with List

To make updates to a record of SQL Server using Entity Framework Core, I query the record I need to update, make changes to the object and then call .SaveChanges() . This works nice and clean.

For example:

var emp = _context.Employee.FirstOrDefault(item => item.IdEmployee == Data.IdEmployee);
emp.IdPosition = Data.IdPosition;
await _context.SaveChangesAsync();

But is there a standard method if I want to update multiple records?

My first approach was using a list passing it to the controller, but then I would need to go through that list and save changes every time, never really finished this option as I regarded it as not optimal.

For now what I do is instead of passing a list to the controller, I pass each object to the controller using a for. (kind of the same...)

for(int i = 0; i < ObjectList.Count; i ++) 
{
    /* Some code */
    var httpResponseObject = await MyRepositories.Post<Object>(url+"/Controller", Object);
}

And then do the same thing on the controller as before, when updating only one record, for each of the records...

I don't feel this is the best possible approach, but I haven't found another way, yet.

What would be the optimal way of doing this?

Your question has nothing to do with Blazor... However, I'm not sure I understand what is the issue. When you call the SaveChangesAsync method, all changes in your context are committed to the database. You don't have to pass one object at a time...You can pass a list of objects

Hope this helps...

Updating records in bulk using Entity Framework or other Object Relational Mapping (ORM) libraries is a common challenge because they will run an UPDATE command for every record. You could try using Entity Framework Plus , which is an extension to do bulk updates.

If updating multiple records with a single call is critical for you, I would recommend just writing a stored procedure and call if from your service. Entity Framework can also run direct queries and stored procedures.

It looks like the user makes some changes and then a save action needs to persist multiple records at the same time. You could trigger multiple AJAX calls—or, if you need, just one.

What I would do is create an endpoint—with an API controller and an action—that's specific to your needs. For example, to update the position of records in a table:

Controller:

/DataOrder

Action:

[HttpPut]
public async void Update([FromBody] DataChanges changes)
{
    foreach(var change in changes)
    {
        var dbRecord = _context.Employees.Find(change.RecordId);
        dbRecord.IdPosition  = change.Position;
    }
    _context.SaveChanges();
}  

public class DataChanges
{
    public List<DataChange> Items {get;set;}

    public DataChangesWrapper()
    {
        Items = new List<DataChange>();
    }
}

public class DataChange
{
    public int RecordId {get;set;}
    public int Position {get;set;}
}

The foreach statement will execute an UPDATE for every record. If you want a single database call, however, you can write a SQL query or have a stored procedure in the database and pass the data as a DataTable parameter instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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