How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core

c# entity-framework entity-framework-core partial partial-classes

Question

I'm using EF Core DbFirst approach. My dbContext is created automatically by Scaffold-DbContext command.

I need to add additional DbSets into a dbContext and add into OnModelCreating method of dbContext some additional code but after each scaffolding that added code are erased and I have to add it each time again.

What I want to do is to create another partial dbContext class and mark protected override void OnModelCreating(ModelBuilder modelBuilder) as a partial method

but get errors:

  1. A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers.
  2. A partial method may not have multiple implementing declarations

Here is a pseudo code:

MyDbContext1.cs - generated by Scaffold-DbContext

public partial class MyDbContext : DbContext
    {
        public MyDbContext()
        {
        }

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

        public virtual DbSet<Client> Clients { get; set; }

        protected override partial void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Client>(entity =>
            {
                // ... some code
            }
        }
    }

MyDbContext2.cs - this code I added each time into dbContext after scaffolding:

public partial class MyDbContext
    {
        public virtual DbSet<JustAnotherEntity> AnotherEntity { get; set; }

        protected override partial void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<JustAnotherEntity>(entity =>
            {
                entity.HasKey(e => new {e.Id, e.IdAction, e.IdState})
                    .ForSqlServerIsClustered(false);
            });
        }
    }
1
18
9/5/2018 9:46:35 AM

Accepted Answer

An alternative would be creating another context class that inherit from MyDbContext that actually include all the custom code. and then use this new class as your context. This way, there is no need to update the generated code.

public class MyDbContext2 : MyDbContext 
{
    public MyDbContext2()
    {
    }

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

    public virtual DbSet<JustAnotherEntity> AnotherEntity { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<JustAnotherEntity>(entity =>
        {
            entity.HasKey(e => new {e.Id, e.IdAction, e.IdState})
                .ForSqlServerIsClustered(false);
        });
    }
}
25
11/13/2018 10:11:31 PM

Popular Answer

EFCore 3 - They FINALLY fixed this!

You can now implement OnModelCreatingPartial in a partial class like this:

public partial class RRStoreContext : DbContext
{
    partial void OnModelCreatingPartial(ModelBuilder builder)
    {
        builder.Entity<RepeatOrderSummaryView>().HasNoKey();
    }
}

If you look at the generated context file - right at the very end you'll see...

 OnModelCreatingPartial(modelBuilder);

Note: This was necessary because I needed to manually add HasNoKey for a stored procedure (with a custom return type that wasn't otherwise scaffolded).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow