Thursday, August 26, 2021

EF Core 6.0: Introducing Migration Bundles

In this post, let's have a look at a new feature that's coming with EF Core 6 in November, 2021. That's EF Core Migration Bundles. So far when we are using EF Core, most of the time we are using dotnet ef migrations script and database update to update the database.

The EF team has come up with this new approach to apply migrations that is by creating an executable with the migrations scripts and once we execute it, it will update the database.

To try out EF Core Migration Bundles, you will need at least the EF Core 6 Preview 7 Version of Tools.

# install
dotnet tool install --global dotnet-ef --version 6.0.0-preview.7.21378.4

# if you have already have it installed, then upgrade
dotnet tool update --global dotnet-ef --version 6.0.0-preview.7.21378.4

Once you have this version installed, you should see a new command.

dotnet ef migrations bundle
dotnet ef migrations bundle
You can see a variety of options that you can use, almost all of them are self-described.

Now let's see things in action. Consider I have the following code.

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContextservices) =>
    {
        services
            .AddDbContext<MyDbContext>(options =>
            {
                options.UseSqlServer(hostContext.Configuration.GetConnectionString(nameof(MyDbContext)));
            });
    })
    .Build();

await host.RunAsync();

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }

    public DbSet<Category> Categories { getset; }

    public DbSet<Product> Products { getset; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Category>(builder =>
        {
            builder
                .HasMany(x => x.Products)
                .WithOne()
                .HasForeignKey(x => x.CategoryId);
        });
    }
}

public class Category
{
    public int Id { getset; }

    public string Name { getset; }

    public ICollection<Product> Products { getset; }
}

public class Product
{
    public int Id { getset; }

    public string Name { getset; }

    public string Description { getset; }

    public int CategoryId { getset; }
}

And I have added some migrations for this DbContext. 

Then I just have to run the bundle command in CLI,

dotnet ef migrations bundle

dotnet ef migrations mundle

Or if you are using VS, run the following command in Package Manager Console. (Note as today with the latest bits, it doesn't work, an issue is already logged: #25555)

Bundle-Migration

Once I do that, it will create an executable named bundle.exe. (EF Core 6 RC 1 will have an option --output that will give us the ability to change the output)

bundle --help

Then I just need to executable the bundle.exe. By default, it will pick up the connection string from appsettings.json. Or you can pass in the connection as an option by doing something like this,

.\bundle.exe --connection "<ConnectionString>"

You can also pass in a target migration if you want to, something like below.

.\bundle.exe 20210825094417_Initial --connection "<ConnectionString>"

So what's the advantage of using migrations bundle.

  • Can be made a self-contained executable with everything needed to run a migration. For example in DevOps pipelines, you don't need to explicitly install dotnet ef.
  • It doesn’t require you to copy source code or install the .NET SDK (only the runtime) and can be integrated as a deployment step in your DevOps pipeline.
Read More:

Sample Code:

Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment