简体   繁体   中英

Entity Framework Update from List Of class

I have below table CustEntry:-

CustID CustName CustAddress AddedBy DateAdded CustStatus

Within this table I have many records.

I am using EF6 From entityframework I want to update only 2 columns of all table records.

For that I have class structure as :-

class CustEntry
{
    public string CustName {get;set;}
    public string CustStatus {get;set;}
}
class RootCustEntry
{
    public List<CustEntry> CustEntry{ get; set; }
}

Note :- I have to update only CustName and CustStatus comming to me through List CustEntry from RootCustEntry object

How can I bulk update the records without using foreach loop?

How can I bulk update the records without using foreach loop?

Unfortunately, this is not supported in Entity Framework out of the box.

However, you can use the batch update functionality in the EntityFramework.Extended (Not supported since 2015 as stated on their github).

So you can check out this EntityFramework Plus (Recomended by the creators of EntityFramework.Extended, as stated on their github).

Batch Update Example code of EntityFramework Plus

// using Z.EntityFramework.Plus; // Don't forget to include this.

// UPDATE all users inactive for 2 years
var date = DateTime.Now.AddYears(-2);
ctx.Users.Where(x => x.LastLoginDate < date)
         .UpdateAsync(x => new User() { IsSoftDeleted = 1 });

PS: If you do not want to use 3rd party libraries then you can write a stored procedure

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