<i id='ikiOY'><tr id='ikiOY'><dt id='ikiOY'><q id='ikiOY'><span id='ikiOY'><b id='ikiOY'><form id='ikiOY'><ins id='ikiOY'></ins><ul id='ikiOY'></ul><sub id='ikiOY'></sub></form><legend id='ikiOY'></legend><bdo id='ikiOY'><pre id='ikiOY'><center id='ikiOY'></center></pre></bdo></b><th id='ikiOY'></th></span></q></dt></tr></i><div id='ikiOY'><tfoot id='ikiOY'></tfoot><dl id='ikiOY'><fieldset id='ikiOY'></fieldset></dl></div>

<legend id='ikiOY'><style id='ikiOY'><dir id='ikiOY'><q id='ikiOY'></q></dir></style></legend>
    <bdo id='ikiOY'></bdo><ul id='ikiOY'></ul>

        <small id='ikiOY'></small><noframes id='ikiOY'>

      1. <tfoot id='ikiOY'></tfoot>

      2. 在 Startup.cs 中添加 DbContextOptions 未注册数据存储

        时间:2023-07-12

          <tbody id='nVrzp'></tbody>

          <small id='nVrzp'></small><noframes id='nVrzp'>

            <bdo id='nVrzp'></bdo><ul id='nVrzp'></ul>
            <legend id='nVrzp'><style id='nVrzp'><dir id='nVrzp'><q id='nVrzp'></q></dir></style></legend>
          • <tfoot id='nVrzp'></tfoot>

                1. <i id='nVrzp'><tr id='nVrzp'><dt id='nVrzp'><q id='nVrzp'><span id='nVrzp'><b id='nVrzp'><form id='nVrzp'><ins id='nVrzp'></ins><ul id='nVrzp'></ul><sub id='nVrzp'></sub></form><legend id='nVrzp'></legend><bdo id='nVrzp'><pre id='nVrzp'><center id='nVrzp'></center></pre></bdo></b><th id='nVrzp'></th></span></q></dt></tr></i><div id='nVrzp'><tfoot id='nVrzp'></tfoot><dl id='nVrzp'><fieldset id='nVrzp'></fieldset></dl></div>

                  本文介绍了在 Startup.cs 中添加 DbContextOptions 未注册数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  My problem is that the below code does not register a data store during startup. This is the specific "error" statement I get in the response from the application:

                  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)
                  

                  In ConfigureServices(IServiceCollection services) I am trying to specify DbContextOptions for my DbContext in a lambda. Code:

                  services.AddEntityFramework(Configuration)
                      .AddSqlServer()
                      .AddDbContext<MyDbContext>(
                          options =>
                          options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"))
                      );
                  

                  In my DbContext, I have a constructor which sends the option to base, code:

                  public MyContext(DbContextOptions options) : base(options) { }
                  

                  My configuration file config.json, which is read at startup, contains this connectionstring:

                  "Data": {
                      "DefaultConnection": {
                          "ConnectionString": "Server=(localdb)\MSSQLLocalDB;Database=MyDbName;Trusted_Connection=True;MultipleActiveResultSets=True;"
                      }
                  }
                  

                  I have previously used

                  protected override void OnConfiguring(DbContextOptions options)
                  {
                      options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));
                  
                  }
                  

                  in my DbContext successfully. It registers the data store and it works properly, but I'd rather use the lambda way.

                  If there is any more information needed, I'll provide it.

                  解决方案

                  Are you injecting your context into your controller or wherever you are using it? I discovered that if you try to new up the context instead of injecting it, it does not use the configuration specified in Startup.cs

                  这篇关于在 Startup.cs 中添加 DbContextOptions 未注册数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 html 帮助程序迁移到 ASP.NET Core 下一篇:将数组从 C# COM 对象传递给 JavaScript?

                  相关文章

                2. <tfoot id='pLqel'></tfoot>
                  <i id='pLqel'><tr id='pLqel'><dt id='pLqel'><q id='pLqel'><span id='pLqel'><b id='pLqel'><form id='pLqel'><ins id='pLqel'></ins><ul id='pLqel'></ul><sub id='pLqel'></sub></form><legend id='pLqel'></legend><bdo id='pLqel'><pre id='pLqel'><center id='pLqel'></center></pre></bdo></b><th id='pLqel'></th></span></q></dt></tr></i><div id='pLqel'><tfoot id='pLqel'></tfoot><dl id='pLqel'><fieldset id='pLqel'></fieldset></dl></div>

                    <bdo id='pLqel'></bdo><ul id='pLqel'></ul>

                    1. <small id='pLqel'></small><noframes id='pLqel'>

                    2. <legend id='pLqel'><style id='pLqel'><dir id='pLqel'><q id='pLqel'></q></dir></style></legend>