简体   繁体   中英

Bulk update add to many-to-many with EF Extended?

I have a situation where I need to add a new item to a property for a group of objects that has a many-to-many relationship. Is there any way to do this in bulk using EntityFramework.Extended? Something like...

Ctx.Foos
   .Where(f => fooIds.Contains(f.FooId))
   .Update(f => f.Bars.Add(bar)) // <-- what would go here???

Obviously, the Update() part is not correct. For the time being, I've worked around it by retrieving the set and looping through. Just wondering if there is a better way.

The short answer is: NO

The Update() method allows only to update properties directly related to the entity. Since you want to update a many or many relations (not a property), this library does not allow to do it.

Disclaimer : I'm the owner of the project Entity Framework Extensions

The best & fastest way if you need performance is by using the Entity Framework Extensions library with the BulkSaveChanges method.

Ctx.Foos
   .Where(f => fooIds.Contains(f.FooId))
   .ToList()
   .ForEach(f => f.Bars.Add(bar))

Ctx.BulkSaveChanges();

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