C# 应用程序上下文DbSet<&燃气轮机;如何从另一个类继承dbset
下面是我们使用的一个简单的DcContext。我们现在使用数百个表,我们希望将我们的数据库集组织成更小的类C# 应用程序上下文DbSet<&燃气轮机;如何从另一个类继承dbset,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,下面是我们使用的一个简单的DcContext。我们现在使用数百个表,我们希望将我们的数据库集组织成更小的类 //Working Example //ApplicationDbContext.cs public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext
//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; }
}
为什么不使用分部类?您仍然可以根据需要命名每个.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; }
}
//ApplicationDbContext.cs
公共分部类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
数据库。请重新创建();
}
//自动控制系统
公共分部类ApplicationDbContext:IdentityDbContext
{
//桌子
公共数据库集车辆{get;set;}
公共DbSet Vans{get;set;}
公共数据库集{get;set;}
}
> p>它似乎是重新设计的候选对象,也许我建议考虑使用多个DbValuts。
对于单个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; }
}
//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; }
}
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; }
}
class VehicleRepository
{
private readonly IAutoDbSets context;
public VehicleRepository(IAutoDbSets context)
{
this.context = context;
}
}