Files
sharp-basic/test/Data.Test/TestDbContext.cs
T
2025-12-30 09:17:25 +08:00

183 lines
6.2 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace Data.Test;
/// <summary>
/// 测试用的数据库上下文
/// 演示如何配置各种类型的实体
/// </summary>
public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}
// 使用基类的实体
public DbSet<Product> Products => Set<Product>();
public DbSet<Category> Categories => Set<Category>();
public DbSet<Tag> Tags => Set<Tag>();
// 自定义实体
public DbSet<CustomProduct> CustomProducts => Set<CustomProduct>();
public DbSet<Region> Regions => Set<Region>();
// 实现接口的实体
public DbSet<Order> Orders => Set<Order>();
public DbSet<Notification> Notifications => Set<Notification>();
public DbSet<Customer> Customers => Set<Customer>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
ConfigureProductEntity(modelBuilder);
ConfigureCategoryEntity(modelBuilder);
ConfigureTagEntity(modelBuilder);
ConfigureCustomProductEntity(modelBuilder);
ConfigureRegionEntity(modelBuilder);
ConfigureOrderEntity(modelBuilder);
ConfigureNotificationEntity(modelBuilder);
ConfigureCustomerEntity(modelBuilder);
}
/// <summary>
/// 配置 Product 实体 (使用 AuditableEntity)
/// </summary>
private void ConfigureProductEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
entity.Property(e => e.Price).HasPrecision(18, 2);
entity.Property(e => e.Description).HasMaxLength(500);
// 添加索引
entity.HasIndex(e => e.Name);
entity.HasIndex(e => e.IsDeleted); // 软删除字段索引(提升查询性能)
});
}
/// <summary>
/// 配置 Category 实体 (使用 Entity 基类)
/// </summary>
private void ConfigureCategoryEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
entity.Property(e => e.Description).HasMaxLength(200);
});
}
/// <summary>
/// 配置 Tag 实体 (使用 EntityInt 基类,自增主键)
/// </summary>
private void ConfigureTagEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Tag>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Id).ValueGeneratedOnAdd(); // 自增
entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
entity.Property(e => e.Color).HasMaxLength(20);
});
}
/// <summary>
/// 配置 CustomProduct 实体 (完全自定义)
/// </summary>
private void ConfigureCustomProductEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CustomProduct>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
entity.Property(e => e.Price).HasPrecision(18, 2);
entity.Property(e => e.CreatedDate).IsRequired();
// 忽略计算属性
entity.Ignore(e => e.IsExpensive);
});
}
/// <summary>
/// 配置 Region 实体 (字符串主键)
/// </summary>
private void ConfigureRegionEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Region>(entity =>
{
entity.HasKey(e => e.Code); // 字符串作为主键
entity.Property(e => e.Code).HasMaxLength(10);
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
entity.Property(e => e.Description).HasMaxLength(500);
});
}
/// <summary>
/// 配置 Order 实体 (实现接口但不使用基类)
/// </summary>
private void ConfigureOrderEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.OrderNumber).IsRequired().HasMaxLength(50);
entity.Property(e => e.TotalAmount).HasPrecision(18, 2);
// 索引
entity.HasIndex(e => e.OrderNumber).IsUnique();
entity.HasIndex(e => e.IsDeleted);
});
}
/// <summary>
/// 配置 Notification 实体 (只实现软删除)
/// </summary>
private void ConfigureNotificationEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Notification>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Title).IsRequired().HasMaxLength(200);
entity.Property(e => e.Content).IsRequired();
entity.HasIndex(e => e.IsDeleted);
});
}
/// <summary>
/// 配置 Customer 实体 (包含值对象)
/// </summary>
private void ConfigureCustomerEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
entity.Property(e => e.Email).IsRequired().HasMaxLength(200);
// 配置值对象 - 方式1: 使用 OwnsOne
entity.OwnsOne(e => e.ShippingAddress, address =>
{
address.Property(a => a.Street).HasMaxLength(200);
address.Property(a => a.City).HasMaxLength(100);
address.Property(a => a.State).HasMaxLength(50);
address.Property(a => a.ZipCode).HasMaxLength(20);
address.Property(a => a.Country).HasMaxLength(50);
});
entity.OwnsOne(e => e.BillingAddress, address =>
{
address.Property(a => a.Street).HasMaxLength(200);
address.Property(a => a.City).HasMaxLength(100);
address.Property(a => a.State).HasMaxLength(50);
address.Property(a => a.ZipCode).HasMaxLength(20);
address.Property(a => a.Country).HasMaxLength(50);
});
entity.HasIndex(e => e.Email);
});
}
}