采用的是net Core 3.1框架下的 的WebAPI项目。
1. 创建ASP.NET Core Web项目
2. 添加NuGet引用包,包如下
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tool
Pomelo.EntityFrameworkCore.MySql
3. 创建继承自DbContext 的DataContext文件
public class DataContext : DbContext
{ public DbSet<OperateLog> OperateLog { get; set; } public DataContext(DbContextOptions<DataContext> options) : base(options)
{ } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseMySql("Server=localhost;database=omc;uid=root;port=3306;pwd=123321");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// base.OnModelCreating(modelBuilder); //Model Mapping用这个
// modelBuilder.ApplyConfigurationsFromAssembly(typeof(DataContext).Assembly); //FluentAPI Mapping用这个
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); }
}
4. 创建实体模型 OperateLog , 及实体映射数据库表的 OperateLogConfig
public class BaseEntity
{
/// <summary>
/// 主键Id
/// </summary>
public long Id { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateDateTime{get;set;}
/// <summary>
/// 修改时间
/// </summary>
public DateTime? UpdateDateTime { get; set; }
/// <summary>
/// 软删除标志
/// </summary>
public bool IsDelete { get; set; }
/// <summary>
/// 预留字段1
/// </summary>
public string ExtFiled1 { get; set; }
/// <summary>
/// 预留字段2
/// </summary>
public string ExtFiled2 { get; set; }
} /// <summary>
/// 系统用户操作日志
/// </summary>
public class OperateLog : BaseEntity
{
/// <summary>
/// 一级菜单名
/// </summary>
public string LevelOneMenuName { get; set; }
/// <summary>
/// 二级菜单名
/// </summary>
public string LevelTwoMenuName { get; set; }
/// <summary>
/// 三级菜单名
/// </summary>
public string LevelThreeeMenuName { get; set; }
/// <summary>
/// 操作备注
/// </summary>
public string OperateRemark { get; set; }
/// <summary>
/// 操作人Id
/// </summary>
public long UserId { get; set; }
/// <summary>
/// 操作人姓名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 操作结果
/// </summary>
public string OperateResult { get; set; }
/// <summary>
/// 操作结果详情
/// </summary>
public string OperateResultInfo { get; set; } }
public class OperateLogConfig : IEntityTypeConfiguration<OperateLog>
{ public void Configure(EntityTypeBuilder<OperateLog> builder)
{
builder.HasComment("系统用户操作日志表");
builder.Property(t => t.Id).HasComment("主键Id"); builder.Property(t => t.LevelOneMenuName).HasComment("一级菜单名").HasMaxLength();
builder.Property(t => t.LevelTwoMenuName).HasComment("二级菜单名").HasMaxLength();
builder.Property(t => t.LevelThreeeMenuName).HasComment("三级菜单名").HasMaxLength();
builder.Property(t => t.OperateRemark).HasComment("操作备注").HasMaxLength();
builder.Property(t => t.UserId).HasComment("操作人Id").HasMaxLength();
builder.Property(t => t.UserName).HasComment("操作人姓名").HasMaxLength();
builder.Property(t => t.OperateResult).HasComment("操作结果").HasMaxLength();
builder.Property(t => t.OperateResultInfo).HasComment("操作结果详情").HasMaxLength(); builder.Property(t => t.CreateDateTime).HasComment("创建时间");
builder.Property(t => t.UpdateDateTime).HasComment("修改时间");
builder.Property(t => t.IsDelete).HasComment("软删除标志 ,true表示删除,false表示未删除").HasDefaultValue(false);
builder.Property(t => t.ExtFiled1).HasComment("预留扩展字段1").HasMaxLength();
builder.Property(t => t.ExtFiled2).HasComment("预留扩展字段2").HasMaxLength();
}
}
5. 在Startup 文件中的ConfigureServices方法中, 配置Mysql 的连接服务
public void ConfigureServices(IServiceCollection services)
{
var connection = "Server=localhost;database=omc;uid=root;port=3306;pwd=123321";
services.AddDbContext<DataContext>(options => options.UseMySql(connection));
services.AddControllers();
}
6. 打开程序包管理控制台, 输入PM 命令,即可得到想要的结果
Add-Migration EFCore
该命令会在程序中自动生成所需的对应数据库的脚本文件,截图如下
update-database EFCore
则会执行上图中的Migrations中的程序文件,会在所连接的数据库中生成对应的表结构,截图如下