Asp.Net core dbcontext issue

asp.net-core entity-framework-core

Question

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
1
11/26/2016 6:17:39 PM

Accepted Answer

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.

1
11/26/2016 6:30:15 PM

Popular Answer

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/



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow