应用程序上下文DbSet<>如何从另一个类继承DbSets

时间:2018-03-11 17:27:43

标签: c# asp.net-core entity-framework-core

以下是我们使用的简单DcContext。我们现在使用数百个表,我们希望将DbSets组织成更小的类

//Working Example
//ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    {
        Database.EnsureCreated();
    }
    //Tables
    public DbSet<Car> Cars { get; set; }
    public DbSet<Van> Vans { get; set; }
    public DbSet<Truck> Trucks { get; set; }
    public DbSet<Repair> Repairs { get; set; }
    public DbSet<Service> Services { get; set; }
    }

我们现在想把它拆分成更小的块:

//Not Working Example
//ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    {
        Database.EnsureCreated();
    }
    //Tables from another class
    public class AutoDbSets :Auto.DbSets { }
    public class RepairDbSets :Repair.DbSets { }
    }

//Auto.cs
public class Auto
{
    public class DbSets 
    {
    //Tables
    public DbSet<Car> Cars { get; set; }
    public DbSet<Van> Vans { get; set; }
    public DbSet<Truck> Trucks { get; set; }

   }
}
//Repair.cs
public class Repair
{
    public class DbSets 
    {
    //Tables
    public DbSet<Repair> Repairs { get; set; }
    public DbSet<Service> Services { get; set; }

   }
}

代码在编译器中看起来不错,但是它不会创建表,而我在ApplicationDbContext中看起来也不行。

问题似乎集中在以下

    //Tables from another class
    public class AutoDbSets :Auto.DbSets { }
    public class RepairDbSets :Repair.DbSets { }

2 个答案:

答案 0 :(得分:1)

为什么不使用部分课程?您仍然可以为每个.CS文件命名,但在内部,您仍然需要使用相同的dbcontext名称。仍然,保持事情更清洁,更简单。

//ApplicationDbContext.cs
    public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    {
        Database.EnsureCreated();
    }

//Auto.cs
public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    //Tables
    public DbSet<Car> Cars { get; set; }
    public DbSet<Van> Vans { get; set; }
    public DbSet<Truck> Trucks { get; set; }

   }

答案 1 :(得分:0)

它似乎是重新设计的候选者,也许我建议考虑使用多个DbContexts。

也可以为单个DbContext使用接口进行多重继承:

public interface IRepairDbSets
{
    DbSet<Repair> Repairs { get; set; }
    DbSet<Service> Services { get; set; }
}

public interface IAutoDbSets
{
    DbSet<Car> Cars { get; set; }
    DbSet<Van> Vans { get; set; }
}

public class ApplicationDbContext : DbContext, IAutoDbSets, IRepairDbSets
{
    public DbSet<Car> Cars { get; set; }
    public DbSet<Van> Vans { get; set; }
    public DbSet<Repair> Repairs { get; set; }
    public DbSet<Service> Services { get; set; }
}

使用这种方法,您只能注入所需的特定DbSet并忽略其他DbSet ......

class VehicleRepository
{
    private readonly IAutoDbSets context;

    public VehicleRepository(IAutoDbSets context)
    {
        this.context = context;
    }

}