从appsettings.json中读取ConnectionString以获取Entity.Framework [英] read ConnectionString from appsettings.json for entity.Framework

查看:124
本文介绍了从appsettings.json中读取ConnectionString以获取Entity.Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从appsettings.json中读取用于实体框架和MySQL的ConnectiongString,但出现错误:

i try to read a ConnectiongString from appsettings.json for entityFramework and MySQL, but i get the error:

System.ArgumentNullException: Value cannot be null.
Parameter name: connectionString

这是我的appsetings.json:

this is my 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();
}

这是我的DBContext:

and this is my DBContext:

public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> dbContextOptions) :
            base(dbContextOptions)
        {

        }
        public DbSet<Person> People { get; set; }
    }

如果我将 MigrationConnection更改为:

if i change the "MigrationConnection" with:

server=127.0.0.1;userid=xxx;pwd=xxx;port=3306;database=xxx;sslmode=none;

会是什么?

推荐答案

您收到错误消息是因为 Configuration.GetConnectionString( MigrationConnection)返回 null 。应当替换为:

You're getting the error because Configuration.GetConnectionString("MigrationConnection") returns null. It should be replaced with:

var connection = Configuration.GetSection("Data").GetConnectionString("MigrationConnection");

或者您可以将appsettings.json的结构更改为:

Or you can change the structure of appsettings.json to:

{
    "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;"
    }  
}

这篇关于从appsettings.json中读取ConnectionString以获取Entity.Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆