我有一个实体数量较多的项目,以及一个未来可以进一步扩展的项目。此时,我想自动化我的 dbset 操作。在这种情况下,我应该创建什么样的生成器?或者自动化这些操作是正确的方法吗?
namespace HospitalManagement.Persistence.Context
{
public class HospitalManagementDbContext : IdentityDbContext<AppUser, AppRole, string>
{
public HospitalManagementDbContext(DbContextOptions options) : base(options)
{
}
#region Common
public DbSet<Address> Addresses { get; set; }
public DbSet<AnyParam> AnyParams { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<County> Counties { get; set; }
public DbSet<DbParameter> DbParameters { get; set; }
public DbSet<DbParameterType> DbParameterTypes { get; set; }
public DbSet<Error> Errors { get; set; }
public DbSet<Domain.Entities.Common.File> Files { get; set; }
public DbSet<Hospital> Hospitals { get; set; }
public DbSet<ItemType> ItemTypes { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Status> Statuses { get; set; }
#endregion
#region Management
public DbSet<Announcement> Announcements { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<ItemChange> ItemChanges { get; set; }
public DbSet<OperationLog> OperationLogs { get; set; }
public DbSet<PdfTemplate> PdfTemplates { get; set; }
public DbSet<ServiceLog> ServiceLogs { get; set; }
#endregion
#region Medical
public DbSet<Medicine> Medicines { get; set; }
public DbSet<MedicineDetail> MedicineDetails { get; set; }
public DbSet<Prescription> Prescriptions { get; set; }
public DbSet<Treatment> Treatments { get; set; }
public DbSet<TreatmentPlan> TreatmentPlans { get; set; }
#endregion
#region Users
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Patient> Patients { get; set; }
public DbSet<Staff> Staffs { get; set; }
public DbSet<Visitor> Visitors { get; set; }
#endregion
}
}
您可以尝试使用反射来自动注册 DbContext 中的所有 DbSet 属性。
首先,您必须创建一个使用反射来注册 DbSet 属性的 DbContext 基类:
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Reflection;
namespace HospitalManagement.Persistence.Context
{
public class BaseDbContext : IdentityDbContext<AppUser, AppRole, string>
{
public BaseDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
RegisterAllEntities(modelBuilder, typeof(IEntity));
}
private void RegisterAllEntities(ModelBuilder modelBuilder, Type interfaceType)
{
var types = Assembly.GetAssembly(typeof(HospitalManagementDbContext))
.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Contains(interfaceType));
foreach (var type in types)
{
modelBuilder.Entity(type);
}
}
public DbSet<T> Set<T>() where T : class
{
return base.Set<T>();
}
}
}
定义
IEntity
接口并确保所有实体都实现它:
namespace HospitalManagement.Domain.Entities
{
public interface IEntity
{
// Common properties can be defined here if needed
}
public class Address : IEntity { /* ... */ }
public class AnyParam : IEntity { /* ... */ }
// Add IEntity to all your entities
}
从基础 DbContext 继承 DbContext:
namespace HospitalManagement.Persistence.Context
{
public class HospitalManagementDbContext : BaseDbContext
{
public HospitalManagementDbContext(DbContextOptions options) : base(options)
{
}
#region Common
public DbSet<Address> Addresses { get; set; }
public DbSet<AnyParam> AnyParams { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<County> Counties { get; set; }
public DbSet<DbParameter> DbParameters { get; set; }
public DbSet<DbParameterType> DbParameterTypes { get; set; }
public DbSet<Error> Errors { get; set; }
public DbSet<Domain.Entities.Common.File> Files { get; set; }
public DbSet<Hospital> Hospitals { get; set; }
public DbSet<ItemType> ItemTypes { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Status> Statuses { get; set; }
#endregion
#region Management
public DbSet<Announcement> Announcements { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<ItemChange> ItemChanges { get; set; }
public DbSet<OperationLog> OperationLogs { get; set; }
public DbSet<PdfTemplate> PdfTemplates { get; set; }
public DbSet<ServiceLog> ServiceLogs { get; set; }
#endregion
#region Medical
public DbSet<Medicine> Medicines { get; set; }
public DbSet<MedicineDetail> MedicineDetails { get; set; }
public DbSet<Prescription> Prescriptions { get; set; }
public DbSet<Treatment> Treatments { get; set; }
public DbSet<TreatmentPlan> TreatmentPlans { get; set; }
#endregion
#region Users
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Patient> Patients { get; set; }
public DbSet<Staff> Staffs { get; set; }
public DbSet<Visitor> Visitors { get; set; }
#endregion
}
}
确保所有实体都继承自
IEntity
接口:
namespace HospitalManagement.Domain.Entities.Common
{
public class File : IEntity { /* ... */ }
}
namespace HospitalManagement.Domain.Entities
{
public class Address : IEntity { /* ... */ }
public class AnyParam : IEntity { /* ... */ }
// Implement IEntity in all your entities
}