【发布时间】:2018-05-27 06:40:05
【问题描述】:
我想将我的DbContext 类拆分成多个部分,以便每个模块都有自己的类,我相信这可以让工作更轻松、更多错误宝座并降低代码复杂性。
我相信我在过去的某个地方在谷歌搜索时看到了它,但是没有找到它。
我的DbContext 在基础设施层中,我的类在域上下文中。那不是问题。我想将映射和配置分成单独的类。我的DbContext 将保持不变,但会被拆分。
我在下面标记了我愿意拆分的代码和平:
public class WestCoreDbContext : DbContext
{
public WestCoreDbContext(DbContextOptions<WestCoreDbContext> options) : base(options)
{
}
#region WOULD LIKE TO SPLIT THIS PART
public virtual DbSet<SoftwareTest> SoftwareTests { get; set; }
public virtual DbSet<SoftwareTestCase> SoftwareTestCases { get; set; }
public virtual DbSet<SoftwareTestCaseStep> SoftwareTestCaseSteps { get; set; }
public virtual DbSet<SoftwareTestCaseStepResult> SoftwareTestCaseStepResults { get; set; }
public virtual DbSet<Position> Positions { get; set; }
#endregion
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region WOULD LIKE TO SPLIT THIS PART
SoftwareTestMapping(modelBuilder);
SoftwareTestCaseMapping(modelBuilder);
SoftwareTestCaseMapping(modelBuilder);
SoftwareTestCaseStepMapping(modelBuilder);
SoftwareTestCaseStepResultsMapping(modelBuilder);
PositionMapping(modelBuilder);
RelationshipsMapping(modelBuilder);
#endregion
modelBuilder.MyOracleNamingConventions();
base.OnModelCreating(modelBuilder);
}
#region WOULD LIKE TO SPLIT THIS PART
private void SoftwareTestMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SoftwareTest>();
}
private void SoftwareTestCaseMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SoftwareTestCase>();
}
private void SoftwareTestCaseStepMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SoftwareTestCaseStep>();
}
private void PositionMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Position>();
}
private void SoftwareTestCaseStepResultsMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SoftwareTestCaseStepResult>();
}
private void RelationshipsMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SoftwareTest>().HasMany(x => x.SoftwareTestCases).WithOne(op => op.SoftwareTest).IsRequired().HasForeignKey(@"StId");
modelBuilder.Entity<SoftwareTestCase>().HasOne(x => x.SoftwareTest).WithMany(op => op.SoftwareTestCases).IsRequired().HasForeignKey(@"StId");
modelBuilder.Entity<SoftwareTestCase>().HasMany(x => x.SoftwareTestCaseSteps).WithOne(op => op.SoftwareTestCase).IsRequired().HasForeignKey("StcId");
modelBuilder.Entity<SoftwareTestCaseStep>().HasOne(x => x.SoftwareTestCase).WithMany(op => op.SoftwareTestCaseSteps).IsRequired().HasForeignKey("StcId");
modelBuilder.Entity<SoftwareTestCaseStep>().HasMany(x => x.SoftwareTestCaseStepResults).WithOne(op => op.SoftwareTestCaseStep).IsRequired().HasForeignKey("StcsId");
modelBuilder.Entity<SoftwareTestCaseStepResult>().HasOne(x => x.SoftwareTestCaseStep).WithMany(op => op.SoftwareTestCaseStepResults).IsRequired().HasForeignKey("StcsId");
}
#endregion
public bool HasChanges()
{
return ChangeTracker.Entries().Any(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted);
}
...
}
}
我不知道拆分 DbContext 的确切方法。 我需要一种方法来做到这一点。你能给我解决办法吗?
【问题讨论】:
-
那么你的问题是什么?
-
我不确定如何将 DBContext 拆分为更小的部分。我想有 1 个 DbContext 但有更小的部分。它对我的关注问题的分离。
-
你尝试过吗?只需复制这个类,给它一个新名称并删除你不需要的部分。
-
您似乎尝试在一个类中执行此操作,这确实有效,但需要重构。最简单的方法是拥有多个 DbContext 类,因为您不能使属性在一个类中不可用。如果您确实有最低工作要求,则接口可能是一种解决方案。但是请注意,这种方法几乎不会影响代码复杂性,并且只能使用几个类来保存模型构建(如果您不想重构 BL 以不使用 DbSet 属性)
-
请注意,
context.Set<T>()将始终提供对上下文中所有映射实体的访问。这可能是少数情况下需要额外的工作单元/通用存储库抽象层的情况之一。另请注意,属于一个上下文类的编译模型在每个应用程序生命周期内只会构建一次。您不能为每个实例编译模型(嗯,这是可能的,但不推荐)。因此,如果这就是您所说的“拆分”的意思,请注意您需要不同的上下文类。
标签: c# entity-framework .net-core dbcontext