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

一、在Startup.cs中注册

var conn = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<MyContext>(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;");
posted @ 2022-03-10 19:04  码农阿亮  阅读(723)  评论(0编辑  收藏  举报