我们可以借助EF Core的SharedTypeEntity,映射一个实体类到多个结构相同的数据库表:
public class User { public int Id { get; set; } public string Name { get; set; } } public class SomeDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseSqlServer(Your.ConnectionString) .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.SharedTypeEntity<User>("Male").ToTable("usr_male"); modelBuilder.SharedTypeEntity<User>("Female").ToTable("usr_female"); } } public class Program { public static void Main() { using (var context = new SomeDbContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.Set<User>("Male").Add(new User {Name = "John"}); context.Set<User>("Female").Add(new User {Name = "Mary"}); context.SaveChanges(); } using (var context = new SomeDbContext()) { Console.WriteLine(context.Set<User>("Male").Single().Name); Console.WriteLine(context.Set<User>("Female").Single().Name); } } }
标签:Core,实体类,Name,EF,Set,context,new,public,SharedTypeEntity From: https://www.cnblogs.com/OpenCoder/p/18374218