简体   繁体   English

实体框架的最佳更新性能

[英]Best Update Perfomance with Entity Framework

I am using the Entity Framework 6.1.3. 我正在使用Entity Framework 6.1.3。 I have a list ob objects (same properties like the database table) How can I reach the best perfomance while doing the update statements? 我有一个ob对象的列表(与数据库表相同的属性),在执行更新语句时如何达到最佳性能?

While perfoming any Insert action I could use the AddRange() Method to Insert all Items of the List. 在执行任何插入操作时,我可以使用AddRange()方法插入列表的所有项目。 is there a possibilty to do this as well on update statements? 是否有可能在更新语句上执行此操作?

At the Moment I'll do a foreach on the list, and then I call the update method: 现在,我将在列表上进行一次foreach,然后调用update方法:

 public void UpdateZeitenPaar(int? pc_c_mandant_id, int? pc_d_zeiten_id, int pc_d_zeiten_paare_id, string personalnummer, DateTime? kommZeit, DateTime? gehZeit, int? kommID, int? gehID, decimal? zeitIst, decimal? zeitPause, int? pc_d_aktivitaet_id, DateTime? datum)
    {
        pc_d_zeiten_paare paar = db.pc_d_zeiten_paare.SingleOrDefault(p => p.pc_c_mandant_id == pc_c_mandant_id && p.pc_d_zeiten_paare_id == pc_d_zeiten_paare_id);
        paar.datum = datum;
        paar.gehZeit = gehZeit;
        paar.geh_pc_d_buchungsdaten_id = gehID;
        paar.kommZeit = kommZeit;
        paar.komm_pc_d_buchungsdaten_id = kommID;
        paar.pc_c_mandant_id = pc_c_mandant_id;
        paar.pc_d_aktivitaet_id = pc_d_aktivitaet_id;
        paar.pc_d_zeiten_id = pc_d_zeiten_id;
        paar.personalnummer = personalnummer;
        paar.zeitIst = zeitIst;
        paar.zeitPause = zeitPause;
        db.SaveChanges();
    }

EF creates on SaveChanges for each data-manipulation on a object a separate query. EF为每个对象上的数据操作在SaveChanges创建一个单独的查询。 Even a deletion of multiple objects results in a creation of multiple DELETE queries. 甚至删除多个对象也会导致创建多个DELETE查询。 You can improve the UI Response time by using async/await : 您可以使用async/await缩短UI响应时间:

public async Task UpdateZeitenPaar()
{
    var paar = await db.pc_d_zeiten_paar.SingleOrDefaultAsync(...);
    // [...]
    await db.SaveChangesAsync();
}

You are getting the item on every update call, this is most likely your problem. 您在每次更新调用中都得到该项目,这很可能是您的问题。

pc_d_zeiten_paare paar = db.pc_d_zeiten_paare.SingleOrDefault(p => p.pc_c_mandant_id == pc_c_mandant_id && p.pc_d_zeiten_paare_id == pc_d_zeiten_paare_id);

You are most likely looping through your items with a foreach calling this method and have the id parameter in some collection. 您最有可能使用foreach调用此方法在项目中循环遍历,并在某些集合中使用id参数。 You could try to eager loading all entities at once to memory that you are going to update. 您可以尝试将所有实体立即加载到要更新的内存中。

// This will load all the instances to the context, so they are already in the memory and no round trip to db is required
// Assuming your myItemList is the list that contains your item types and the id's of the items you are going to update
var materializedList = db.pc_d_zeiten_paare.Where(p => myItemList.Any(myp => myp.pc_d_zeiten_paare_id == p.pc_d_zeiten_paare_id).ToList();

// do your normal update logic here

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

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