EF Core中使用数据库连接字符串的两种方式


一、在Startup.cs中注册

var conn = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext(options => options.UseMySQL(conn));

二、在MyDBContext.cs中重写OnConfiguring方法

protected override void OnConfiguring(DbContextOptionsBuilder options)
  => options.UseMySql("server=localhost;userid=root;pwd=xxx;port=3306;database=world;");