在Startup.cs中添加DbContextOptions而不註冊數據存儲


我的問題是下面的代碼在啟動期間沒有註冊數據存儲。這是我從應用程序的響應中得到的特定“錯誤”語句:

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。碼:

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)

在我的DbContext中,我有一個構造函數,它將選項發送到base,代碼:

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)

我的配置文件config.json在啟動時讀取,包含以下連接字符串:

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)

我以前用過

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)

在我的DbContext中成功。它註冊數據存儲並且它正常工作,但我寧願使用lambda方式。

如果需要更多信息,我會提供。

熱門答案

您是否將您的上下文注入您的控制器或您使用它的任何地方?我發現如果你嘗試新建上下文而不是注入它,它就不會使用Startup.cs中指定的配置





許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因