数据库生成实体类代码
NuGet:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Tools
在程序包管理控制台中执行一下命令
Scaffold-DbContext "Server=【地址】;Database=【数据库名字】;Uid=【用户名】;Pwd=【密码】;" Pomelo.EntityFrameworkCore.MySql -OutputDir Entity -Context AmpwensnDbContext -ContextDir / -Force -DataAnnotations
配置文件连接字符串
Startup.cs文件:
public void ConfigureServices(IServiceCollection services){
services.AddDbContext<AmpwensnDbContext>(options => {
options.UseMySql(Configuration.GetConnectionString("Chinook"),
Microsoft.EntityFrameworkCore.ServerVersion.Parse("5.7.19-mysql"));
});
}
services.AddTransient<DbContext, AmpwensnDbContext>();
appsettings.json文件:
{
"ConnectionStrings": {
"Chinook": "server=【地址】;database=【数据库名】;uid=【用户名】;pwd=【密码】"
}
}
控制台输出SQL语句
NuGet:
- Microsoft.Extensions.Logging.Console
在生成的DbContext 中修改一下代码
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(LoggerFactory.Create(configure=> { configure.AddConsole();}));
}
标签:Core,EntityFrameworkCore,数据库,Entity,Framework,services,Chinook,Microsoft
From: https://www.cnblogs.com/tdxm/p/17566020.html