ASP.NET Core dbcontext问题 [英] Asp.Net core dbcontext issue

查看:139
本文介绍了ASP.NET Core dbcontext问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Asp.net核心应用程序,并尝试连接到数据库。在startup.cs文件中的ConfigureServices方法中的以下代码行中出现错误。我得到的错误是值不能为null。似乎无法在web.config文件中找到CustomerOrderEntities键。不确定问题出在哪里

I am creating Asp.net core application and trying to connect to the database. I am getting an error at the following line of code in ConfigureServices method in the startup.cs file. The error that I am getting is the value cannot be null. It seems like it cant find the CustomerOrderEntities key in the web.config file. Not sure what the problem is

services.AddDbContext<CustomerOrderEntities>(options =>
                  options.UseSqlServer(Configuration.GetConnectionString("CustomerOrderEntities")));

Startup.cs

 public IServiceProvider ConfigureServices(IServiceCollection services)
        {
services.AddDbContext<CustomerOrderEntities>(options =>
              options.UseSqlServer(Configuration.GetConnectionString("CustomerOrderEntities")));

 AutoMapperConfiguration.Configure();

            // Create the container builder.
            var builder = new ContainerBuilder();

            // Register dependencies, populate the services from
            // the collection, and build the container. If you want
            // to dispose of the container at the end of the app,
            // be sure to keep a reference to it as a property or field.


            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
            builder.RegisterType<DbFactory>().As<IDbFactory>();
            builder.RegisterType<CustomerRepository>().As<ICustomerRepository>();
            builder.RegisterType<ProductRepository>().As<IProductRepository>();
            builder.RegisterType<OrderRepository>().As<IOrderRepository>();



            builder.Populate(services);
            this.ApplicationContainer = builder.Build();

            // Create the IServiceProvider based on the container.
            return new AutofacServiceProvider(this.ApplicationContainer);
          }

Web.Config

<connectionStrings>

  <add name="CustomerOrderEntities" connectionString="metadata=res://*/EF.CustomerOrderContext.csdl|res://*/EF.CustomerOrderContext.ssdl|res://*/EF.CustomerOrderContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=test-PC\MSSQLSERVER2014;initial catalog=CodeFirstTest;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>


推荐答案

ASP.NET Core中的连接字符串在appsettings中定义.json,例如:

Connection string in ASP.NET Core is defined in appsettings.json, for example:

"ConnectionStrings": {
    "CustomerOrderEntities": "Server=(localdb)\\mssqllocaldb;Database=aspnet-a3e892a5-6a9c-4090-bc79-fe8c79e1eb26;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

如果您的连接字符串名称为CustomerOrderEntities,则会得到null,可能不存在,请检查appsettings.json。

In your case name of connecting string is CustomerOrderEntities, you get null, probably it's not there, check you appsettings.json.

这篇关于ASP.NET Core dbcontext问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆