我的問題是下面的代碼在啟動期間沒有註冊數據存儲。這是我從應用程序的響應中得到的特定“錯誤”語句:
An unhandled exception occurred while processing the request.
InvalidOperationException: No data stores are configured. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.
Microsoft.Data.Entity.Storage.DataStoreSelector.SelectDataStore(ServiceProviderSource providerSource)
在ConfigureServices(IServiceCollection服務)中,我試圖在lambda中為我的DbContext指定DbContextOptions。碼:
services.AddEntityFramework(Configuration)
.AddSqlServer()
.AddDbContext<MyDbContext>(
options =>
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"))
);
在我的DbContext中,我有一個構造函數,它將選項發送到base,代碼:
public MyContext(DbContextOptions options) : base(options) { }
我的配置文件config.json在啟動時讀取,包含以下連接字符串:
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=MyDbName;Trusted_Connection=True;MultipleActiveResultSets=True;"
}
}
我以前用過
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));
}
在我的DbContext中成功。它註冊數據存儲並且它正常工作,但我寧願使用lambda方式。
如果需要更多信息,我會提供。