一、代码框架搭建
搭建如下代码架构:
重点含EntityFrameworkCore工程,该工程中包含AppDbContext.cs和数据表实体AggregateObject
1、AppDbContext 代码案例
//AppDbContext 代码案例
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// 增加以下代码配置
modelBuilder.ConfigDatabaseDescription();
}
public DbSet<CFUserAggregate> CFUserAggregate { get; set; } // 示例:User 是你的实体类
// 添加其他 DbSet<T> 来表示其他数据表
}
}
2、实体案例
1)AggregateRootBase类:
存放表公共字段,例状态、创建时间等
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace EntityFrameworkCore
{
public abstract class AggregateRootBase
{
[Key] // 主键
[StringLength(36)] // 字符串长度限制
[DbDescription("主键ID")]
public Guid? Id { get; set; }
[StringLength(36)]
[DbDescription("创建人ID")]
public Guid? CreateUserGuid { get; set; }
[Required]
[DbDescription("创建时间")]
public DateTime? CreateDateTime { get; set; }
[StringLength(36)]
[DbDescription("修改人ID")]
public Guid? ModifyUserGuid { get; set; }
[DbDescription("修改时间")]
public DateTime? ModifyDateTime { get; set; }
}
}
2)CFUserAggregate 实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace EntityFrameworkCore
{
[Table("CF_User")]
[DbDescription("用户表")]
public class CFUserAggregate : AggregateRootBase
{
[Required]
[StringLength(15)] // 字符串长度限制
[DbDescription("登录人手机号码")]
public string PhoneNumber { get; set; }
[Required]
[StringLength(100)] // 字符串长度限制
[DbDescriptio