简体   繁体   English

带有列表的 EF Core 更新

[英]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() .要使用 Entity Framework Core 更新 SQL 服务器的记录,我查询需要更新的记录,对 object 进行更改,然后调用.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.我的第一种方法是使用一个列表将其传递给 controller,但随后我需要通过该列表 go 并每次保存更改,从未真正完成此选项,因为我认为它不是最佳的。

For now what I do is instead of passing a list to the controller, I pass each object to the controller using a for.现在我所做的不是将列表传递给 controller,而是使用 for 将每个 object 传递给 controller。 (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...然后像以前一样在 controller 上做同样的事情,当只更新一条记录时,对于每条记录......

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.您的问题与 Blazor 无关......但是,我不确定我是否理解问题所在。 When you call the SaveChangesAsync method, all changes in your context are committed to the database.当您调用 SaveChangesAsync 方法时,您的上下文中的所有更改都将提交到数据库。 You don't have to pass one object at a time...You can pass a list of objects您不必一次传递一个 object...您可以传递对象列表

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.使用 Entity Framework 或其他 Object 关系映射 (ORM) 库批量更新记录是一个常见的挑战,因为它们将为每条记录运行UPDATE命令。 You could try using Entity Framework Plus , which is an extension to do bulk updates.您可以尝试使用Entity Framework Plus ,这是进行批量更新的扩展。

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. Entity Framework 还可以运行直接查询和存储过程。

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.您可以触发多个 AJAX 调用,或者,如果需要,只触发一个。

What I would do is create an endpoint—with an API controller and an action—that's specific to your needs.我要做的是使用 API controller 和特定于您的需求的操作创建一个端点。 For example, to update the position of records in a table:例如,要更新表中记录的 position:

Controller: 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. foreach语句将为每条记录执行一次UPDATE 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.但是,如果您想要单个数据库调用,则可以编写 SQL 查询或在数据库中使用存储过程并将数据作为DataTable参数传递。

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

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