Asp.Net core dbcontext issue

不打扰是莪最后的温柔 提交于 2020-01-15 11:07:00

问题


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>

回答1:


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"
  },

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




回答2:


Your connectionstring in Appsettings.json is definitely missing. You cannot set connection string in web.config in Asp.net core whether you are targeting full .net framework or .net core. Alternative you type the connection string value in services.AddDbContext(options => options.UseSqlServer("Your Connectionstring")); for more information check here;

https://docs.microsoft.com/en-us/ef/core/



来源:https://stackoverflow.com/questions/40821459/asp-net-core-dbcontext-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!