![](/img/trans.png)
[英]Any way to detect when DbContext.OnConfiguring() is being called from Package Manager Console?
[英]EntityFramework core `OnConfiguring` not being called on initialization
我已經為這個問題苦苦掙扎了幾天,但我仍然無法弄清楚為什么會發生這種情況。 我正在創建 EntityFramework DbContext
的新實例,並且沒有調用OnConfiguring
方法。 我已經閱讀了這篇文章,不幸的是這沒有幫助。 這是我的代碼大綱
public class MyContext : DbContext
{
public MyContext()
: base()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
int a = 12;
}
}
這就是我從 ASP.net 中的控制器調用它的方式
MyContext b = new MyContext();
但是OnConfiguring
沒有被調用。 我正在使用 EntityFramework 核心 2.2.4 nuget 包。 關於我可以嘗試的事情的任何建議? 這可能會幫助我縮小這個問題的范圍?
您最好使用 Scaffold-DbContext 為您自動生成所有內容。 你需要從nugets下載4
1) EntityFrameworkCore
2) EntityFrameworkCore.Design
3) EntityFrameworkCore.Tools
4) EntityFrameworkCore.SqlServer
打開工具 > NuGet 包管理器 > 包管理器控制台。 並在控制台中輸入以下內容。
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.