简体   繁体   中英

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 ?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM