MistyD
MistyD

Reputation: 17233

EntityFramework core `OnConfiguring` not being called on initialization

I have been struggling with this issue for a few days now and I am still unable to figure out why this might be happening. I am creating a new instance of EntityFramework DbContext and the method OnConfiguring is not being called. I have read this post and unfortunately that does not help. This is an outline of my code

 public class MyContext : DbContext
    {
        public MyContext()
            : base()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            int a = 12;       
        }

    }

This is how I am calling it from my controller in ASP.net

MyContext b = new MyContext();

However OnConfiguring is not being called. I am using EntityFramework core 2.2.4 nuget package. Any suggestions on things that I can try ? that might help me narrow down this issue ?

Upvotes: 0

Views: 1100

Answers (1)

Asherguru
Asherguru

Reputation: 1741

You better use Scaffold-DbContext to generate everything automatically for you. You need to download 4 from nugets

1) EntityFrameworkCore

2) EntityFrameworkCore.Design

3) EntityFrameworkCore.Tools

4) EntityFrameworkCore.SqlServer

Open Tools > NuGet Package Manager > Package Manager Console. And enter this below in console.

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext"

Upvotes: 1

Related Questions