DEV Community

Cover image for New in Entity Framework 7: Bulk Operations with ExecuteDelete and ExecuteUpdate
Tim Deschryver
Tim Deschryver

Posted on • Edited on • Originally published at timdeschryver.dev

New in Entity Framework 7: Bulk Operations with ExecuteDelete and ExecuteUpdate

Follow me on Twitter at @tim_deschryver | Subscribe to the Newsletter | Originally published on timdeschryver.dev.


Read the TLDR version on timdeschryver.dev

Version 7 of Entity Framework includes some popular features that have been asked for, one of which is Bulk Operations.
This feature came to my attention from a tweet by Julie Lerman, and I had to try it out for myself.

Why

So why is this feature needed if we already can update and delete entities?
The key word here is performance. This is a theme that has been on the top of the list when it comes to new EF versions, and this time it is no different.

The added methods improve the performance in multiple ways.
Instead of first retrieving the entities and having all the entities in memory before we can perform an action on them, and lastly committing them to SQL. We now can do this with just a single operation, which results in one SQL command.

Let's take a look at how this looks like in code.

Setting the stage

Before we dive into the examples, let's first set up our SQL database and populate 3 tables:

  • one to hold persons
  • another one is for addresses (a person has an address)
  • and the last one to store pets (a person has a collection of pets)
using Microsoft.EntityFrameworkCore;

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);
}

static void SetupAndPopulate(NewInEFContext context)
{
    context.Database.EnsureDeleted();
    context.Database.EnsureCreated();
    context.Persons.AddRange(Enumerable.Range(1, 1_000).Select(i =>
    {
        return new Person
        {
            FirstName = $"{nameof(Person.FirstName)}-{i}",
            LastName = $"{nameof(Person.LastName)}-{i}",
            Address = new Address
            {
                Street = $"{nameof(Address.Street)}-{i}",
            },
            Pets = Enumerable.Range(1, 3).Select(i2 =>
            {
                return new Pet
                {
                    Breed = $"{nameof(Pet.Breed)}-{i}-{i2}",
                    Name = $"{nameof(Pet.Name)}-{i}-{i2}",
                };
            }).ToList()
        };
    }));

    context.SaveChanges();
}

public class NewInEFContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Pet> Pets { get; set; }
    public DbSet<Address> Addresses { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options
            .UseSqlServer("Connectionstring");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>()
           .Property<long>("PersonId");

        modelBuilder.Entity<Pet>()
            .Property<long>("PersonId");
    }
}

public class Person
{
    public long PersonId { get; set; }
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";
    public Address? Address { get; set; }
    public List<Pet> Pets { get; set; } = new List<Pet>();
}

public class Address
{
    public long AddressId { get; set; }
    public string Street { get; set; } = "";
}

public class Pet
{
    public long PetId { get; set; }
    public string Breed { get; set; } = "";
    public string Name { get; set; } = "";
}
Enter fullscreen mode Exit fullscreen mode

ExecuteDelete and ExecuteDeleteAsync

Now that we got that out of our way, let's dive into ExecuteDelete and ExecuteDeleteAsync.

To delete a set of entities in bulk, filter out the entities that you want to delete by using the Where method (this is similar to before).
Then, invoke the ExecuteDelete method on the collection of entities to be deleted.

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    context.Pets
           .Where(p => p.Name.Contains("1"))
           .ExecuteDelete();
}
Enter fullscreen mode Exit fullscreen mode

Let's also take a look at the SQL statement that this generates:

DELETE FROM [p]
FROM [Pets] AS [p]
WHERE [p].[Name] LIKE N'%1%'
Enter fullscreen mode Exit fullscreen mode

As you can see, it simply generates one SQL statement to delete the entities that match the condition.
The entities also aren't kept in memory anymore.
Nice, simple, and efficient!

Cascade delete

Let's take a look at another example, and let's remove some people that hold references to addresses and pets.
By deleting the person, we also delete the address and pets because the delete statement cascades to the foreign tables.

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    context.Persons
           .Where(p => p.PersonId <= 500)
           .ExecuteDelete();
}
Enter fullscreen mode Exit fullscreen mode

Similar to before, this results in the following SQL statement:

DELETE FROM [p]
FROM [Persons] AS [p]
WHERE [p].[PersonId] <= CAST(500 AS bigint)
Enter fullscreen mode Exit fullscreen mode

Number of rows affected

It's also possible to see how many rows were affected by the delete operation, ExecuteDelete returns the number of rows affected.

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    var personsDeleted =
        context.Persons
           .Where(p => p.PersonId <= 100)
           .ExecuteDelete();
}
Enter fullscreen mode Exit fullscreen mode

In the expression above, the personsDeleted variable is equal to 100.

ExecuteUpdate and ExecuteUpdateAsync

Now that we've seen how to delete entities, let's explore how to update them.
Just like ExecuteDelete, we first have to filter the entities that we want to update, and then invoke ExecuteUpdate.

To update entities we need to use the new SetProperty method.
The first argument of SetProperty selects the property that has to be updated via a lambda, and the second argument is the new value of that property also by using a lambda.

For example, let's set the last name of the persons to "Updated".

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    context.Persons
           .Where(p => p.PersonId <= 1_000)
           .ExecuteUpdate(p => p.SetProperty(x => x.LastName, x => "Updated"));
}
Enter fullscreen mode Exit fullscreen mode

Which results in the corresponding SQL statement:

UPDATE [p]
    SET [p].[LastName] = N'Updated'
FROM [Persons] AS [p]
WHERE [p].[PersonId] <= CAST(1000 AS bigint)
Enter fullscreen mode Exit fullscreen mode

We can also access the values of an entity and use that to create a new value.

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    context.Persons
           .Where(p => p.PersonId <= 1_000)
           .ExecuteUpdate(p => p.SetProperty(x => x.LastName, x => "Updated" + x.LastName));
}
Enter fullscreen mode Exit fullscreen mode

Resulting in the following SQL statement:

UPDATE [p]
    SET [p].[LastName] = N'Updated' + [p].[LastName]
FROM [Persons] AS [p]
WHERE [p].[PersonId] <= CAST(1000 AS bigint)
Enter fullscreen mode Exit fullscreen mode

Updating multiple values at once

We can even update multiple properties at once by invoking SetProperty multiple times.

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    context.Persons
           .Where(p => p.PersonId <= 1_000)
           .ExecuteUpdate(p =>
                p.SetProperty(x => x.LastName, x => "Updated" + x.LastName)
                 .SetProperty(x => x.FirstName, x => "Updated" + x.FirstName));
}
Enter fullscreen mode Exit fullscreen mode

And again, the corresponding SQL statement:

UPDATE [p]
    SET [p].[FirstName] = N'Updated' + [p].[FirstName],
    [p].[LastName] = N'Updated' + [p].[LastName]
FROM [Persons] AS [p]
WHERE [p].[PersonId] <= CAST(1000 AS bigint)
Enter fullscreen mode Exit fullscreen mode

Number of rows affected

Just like ExecuteDelete, ExecuteUpdate also returns the number of rows affected.

using (var context = new NewInEFContext())
{
    SetupAndPopulate(context);

    var personsUpdated =
        context.Persons
           .Where(p => p.PersonId <= 1_000)
           .ExecuteUpdate(p => p.SetProperty(x => x.LastName, x => "Updated"));
}
Enter fullscreen mode Exit fullscreen mode

Note that updating nested entities is not supported.

More Updates in Entity Framework 7

For the complete list of new features, see the EF 7 plan.


Follow me on Twitter at @tim_deschryver | Subscribe to the Newsletter | Originally published on timdeschryver.dev.

Top comments (2)

Collapse
 
bpkinez profile image
Branislav Petrović

Great and consice post! I like this new features in EF7.

I spotted small typo in "Updating multiple values at once" section. You should change ExecuteUpdate for SetProperty in "We can even update multiple properties at once by invoking ExecuteUpdate multiple times." sentence.

Thanks!

Collapse
 
timdeschryver profile image
Tim Deschryver

👀 Thanks!