Application Context DbSet<> How to Inherit DbSets from another class

asp.net-core c# entity-framework-core

Question

The following is a simple DcContext that we use. We are now using hundreds of tables and we want to organize our DbSets into smaller classes

//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; }
    }

We now want to split it into smaller chunks:

//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; }

   }
}

The code looks good in the compiler but it will not create the tables and I cannot seem them in my ApplicationDbContext.

The problem appears to focus on the following

    //Tables from another class
    public class AutoDbSets :Auto.DbSets { }
    public class RepairDbSets :Repair.DbSets { }
1
1
3/13/2018 5:13:33 PM

Accepted Answer

Why not use partial classes? You can still name each .CS file what you want, but within, you still need to use the same name of the dbcontext. Still, keeps things cleaner and simpler.

//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
3/11/2018 9:49:10 PM

Popular Answer

It seems like candidate for redesign, perhaps I'd suggest to consider using multiple DbContexts.

It is also possible to use interfaces for multiple inheritance for single 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; }
}

With this approach you can inject only specific DbSets that is needed and ignore others...

class VehicleRepository
{
    private readonly IAutoDbSets context;

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

}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow