Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I need to add some new DbSets into a dbContext and add into OnModelCreating method 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) method as partial

but get errors:

A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers.

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);         });     } } 
like image 965
Dmitry Stepanov Avatar asked Sep 05 '18 09:09

Dmitry Stepanov


People also ask

Could you describe define DbContext class in Entity Framework Core?

DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database.

What is DbContext and DbSet in Entity Framework?

DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table. Your code sample doesn't fit the expected pattern.

What is OnModelCreating method?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


1 Answers

EFCore 3 - They FINALLY fixed this!

You can now implement OnModelCreatingPartial in a partial class like this. Note the partial keyword on both the class and method:

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 of OnModelCreating(...) you'll see...

 OnModelCreatingPartial(modelBuilder); 

Note: I use scaffolding, but I needed to manually add HasNoKey for a stored procedure (with a custom return type that wasn't otherwise scaffolded).

like image 176
Simon_Weaver Avatar answered Sep 22 '22 08:09

Simon_Weaver