Upgrading .net entity framework to EF Core seems that base and OnModelCreating are not available

c# entity-framework-core


I have been upgrading from .net 4.6 application to .net core 2.1.x and some of these objects and methods are just not available.


  1. Issues with :base("name=mydbentities")
  2. OnModelCreating(DbModelBuilder modelBuilder)
  3. base:(nameOrConnectionString)

From the old .net code base are 2 partial classes

public partial class OpsCentralEntities : DbContext
    public OpsCentralEntities() : base("name=OpsCentralEntities")

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        throw new UnintentionalCodeFirstException();

In the above code

Error CS1503 Argument 1: cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'

then this

'OpsCentralEntities.OnModelCreating(DbModelBuilder)': no suitable method found to override

Then with the other partial class

public partial class OpsCentralEntities : IDbContext
    public OpsCentralEntities(string nameOrConnectionString)
        : base(nameOrConnectionString)
    { }

Cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'

8/23/2018 4:46:03 AM

Accepted Answer

On model creating is available

OnModelCreating(DbModelBuilder modelBuilder) -> OnModelCreating(ModelBuilder modelBuilder) 

Constructor parameter accept options

base("name=mydbentities")-> YourContextContext(DbContextOptions options) : base(options)

There will be problem if you are using SqlQuery because now you need to add it to model

modelBuilder.Query<YourModel>() //to OnModelCreating(ModelBuilder modelBuilder)


And last problem i got was many to many should be using Extra type

8/23/2018 4:58:37 AM

Related Questions


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