ASP.NET Core5.0 MVC中 Pomelo.EntityFrameworkCore.MySql包在框架net5.0怎么连接数据库
appsettings.json
Startup.cs
里面的代码怎么写才能连接mysql
appsettings.json
Startup.cs
里面的代码怎么写才能连接mysql
首先不论你使用什么支持MYSQL的nuget,你要先安装Microsoft.EntityFrameworkCore
version 5.0.17。
创建好对应的DbContext, 例如:
public class MyDBContext : DbContext
{
public MyDBContext(DbContextOptions<MyDBContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
}
然后安装MySql.EntityFrameworkCore
version 5.0.13。 参考:
MySQL :: MySQL Connector/NET Developer Guide :: 7.2 Entity Framework Core Support
https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html
更改你的Startup.cs代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MyDBContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("WebApplicationContext")));
}
}