Hello everyone,
I'm still in a phase of preparing a website and I am creating the first tables with Entity Framework 6. I'm developing with Visual Stuido 2015 Community in ASP.NET MVC core upgraded to .Net 1.1 Core. The problem origined when I tried to run the command "dotnet ef migrations add FirstMigrations" which continues to report me "Value cannot be null.Parameter name: connectionString", but in reality it is inside both "applicationsettings.json" that the "webconfig".
Only when in the instruction "services.AddDbContext <ApplicationIdentityDbContext> (options => options.UseSqlServer ("Server = servername; Database = databasename; Trusted_Connection = True; MultipleActiveResultSets = true "));" I explicitly set the connection string to the command has been successful. Instead with the statement: "services.AddDbContext <ApplicationIdentityDbContext> (options => options .UseSqlServer (config.GetConnectionString (" connectionString "))) this fails. Below the code of the following files:
1. appsettings.json
{"Logging": {"IncludeScopes": false,"LogLevel": {"Default": "Debug","System": "Information","Microsoft": "Information" } }, "ConnectionStrings": {"connectionStringName": ["Server=servername; Database=databasename; Trusted_Connection=True;","MultipleActiveResultSets=true"] } }
2. Web.config
<?xml version="1.0" encoding="utf-8"?><configuration><!-- Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 --><system.webServer><handlers><add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/></handlers><!--<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/> --></system.webServer><connectionStrings><add name="connectionStringName" connectionString="Server=servername;Database=databasename;Integrated Security=true;MultipleActiveResultSets=true" /></connectionStrings></configuration>
3. Startup.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; using WebCSI.Models; namespace WebCSI { public class Startup { public IConfigurationRoot config; //public IConfigurationRoot Configuration { get; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); config = builder.Build(); } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationIdentityDbContext>(options => options .UseSqlServer("Server=servername; Database=databasename; Trusted_Connection=True;MultipleActiveResultSets=true")); // .UseSqlServer(config.GetConnectionString("connectionStringName"))); services.AddIdentity<ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores<ApplicationIdentityDbContext>() .AddDefaultTokenProviders(); // Add framework services. services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(config.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); // Inserito per la gestione delle Identity app.UseIdentity(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } }
Regards,
Paolo