我尝试从appsettings.json中读取EntityFramework和MySQL的connectiongstring,但我得到了错误:
System.ArgumentNullException: Value cannot be null.
Parameter name: connectionString
这是我的appsetings.json:
{
"Data": {
"ConnectionStrings": {
"DefaultConnection": "server=database;userid=hawai;pwd=mysecret;port=3306;database=identity;sslmode=none;",
"MigrationConnection": "server=127.0.0.1;userid=hawai;pwd=mysecret;port=3306;database=identity;sslmode=none;"
}
}
}
这是我的初创公司:
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString("MigrationConnection");
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(Configuration.GetConnectionString("MigrationConnection")));
services.AddMvc();
}
这是我的数据库内容:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> dbContextOptions) :
base(dbContextOptions)
{
}
public DbSet<Person> People { get; set; }
}
如果我将“MigrationConnection”更改为:
server=127.0.0.1;userid=xxx;pwd=xxx;port=3306;database=xxx;sslmode=none;
可能是什么?