[英]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.我正在创建 EntityFramework DbContext
的新实例,并且没有调用OnConfiguring
方法。 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这就是我从 ASP.net 中的控制器调用它的方式
MyContext b = new MyContext();
However OnConfiguring
is not being called.但是OnConfiguring
没有被调用。 I am using EntityFramework core 2.2.4 nuget package.我正在使用 EntityFramework 核心 2.2.4 nuget 包。 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.您最好使用 Scaffold-DbContext 为您自动生成所有内容。 You need to download 4 from nugets你需要从nugets下载4
1) EntityFrameworkCore 1) EntityFrameworkCore
2) EntityFrameworkCore.Design 2) EntityFrameworkCore.Design
3) EntityFrameworkCore.Tools 3) EntityFrameworkCore.Tools
4) EntityFrameworkCore.SqlServer 4) EntityFrameworkCore.SqlServer
Open Tools > NuGet Package Manager > Package Manager Console.打开工具 > NuGet 包管理器 > 包管理器控制台。 And enter this below in console.并在控制台中输入以下内容。
Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.