options.UseSqlServer returns Error in startup.cs

asp.net-core asp.net-core-mvc entity-framework-core

Question

I'm developing a test Project using asp.net core. For Connecting to database I'm using Entity framework. I add appsetings.json file to the project and change the connection string as follows:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\mihanrayan;Initial Catalog=SportsStore;User = sa; Password = 123456"
  }
}

sql version is : mssql 2008 r2

in startup.cs class I add codes below:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddDbContext<ApplicationDbContext>(options =>
               options.UseSqlServer(
               Configuration["Data:SportStoreProducts:ConnectionString"]));
            services.AddTransient<IProductRepository, EFProductRepository>();

        }

but

options.UseSqlServer(
               Configuration["Data:SportStoreProducts:ConnectionString"]));

returns this error:

An exception of type 'System.ArgumentNullException' occurred in Microsoft.EntityFrameworkCore.SqlServer.dll but was not handled in user code

Additional information: Value cannot be null.

1
1
7/25/2018 9:02:18 AM

Popular Answer

It should be like as shown below:

 services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
1
7/25/2018 4:16:45 AM


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