What is the recommended practice to update or delete multiple entities in EntityFramework?

后端 未结 3 567
滥情空心
滥情空心 2021-01-17 07:29

In SQL one might sometimes write something like

DELETE FROM table WHERE column IS NULL

or

UPDATE table SET column1=valu         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-17 08:01

    Entity Framework Extended Library helps to do this.

    Delete

    //delete all users where FirstName matches
    context.Users.Delete(u => u.FirstName == "firstname");
    

    Update

    //update all tasks with status of 1 to status of 2
    context.Tasks.Update(
        t => t.StatusId == 1, 
        t2 => new Task {StatusId = 2});
    
    //example of using an IQueryable as the filter for the update
    var users = context.Users.Where(u => u.FirstName == "firstname");
    context.Users.Update(users, u => new User {FirstName = "newfirstname"});
    

    https://github.com/loresoft/EntityFramework.Extended

提交回复
热议问题