2016-11-16 88 views
0

我很想知道是否有可能將這兩個下面的類合併成一個類。是否可以從兩個上下文創建單個類?

第一的DbContext:

public class ApplicationDbContext : IdentityDbContext<BusinessAccount> 
{ 
    public ApplicationDbContext() 
     : base("MySQLConnectionString", throwIfV1Schema: false) 
    { 
    } 

    public DbSet<Sector> Sectors { get; set; } 

    public DbSet<Campus> Campuss { get; set; } 
    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 

二的DbContext:

public class ApplicationDbContext :DbContext 
{ 
    public ApplicationDbContext() 
     : base("MySQLConnectionString") 
    { 
    } 

    public DbSet<Sector> Sectors { get; set; } 

    public DbSet<Campus> Campuss { get; set; } 
    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 
+1

恕我直言,如果'IdentityDbContext '從'DbContext'派生,第二個類可以合併到第一個。 –

回答

0

繼承IdentityDbContext從的DbContext

public class IdentityDbContext<T1>: DbContext 
{ 
    // Implementation goes here 
} 

繼承ApplicationDbContextIdentityDbContext<BusinessAccount>

public class ApplicationDbContext : IdentityDbContext<BusinessAccount> 
{ 
    public ApplicationDbContext() 
     : base("MySQLConnectionString", throwIfV1Schema: false) 
    { 
    } 

    public DbSet<Sector> Sectors { get; set; }  
    public DbSet<Campus> Campuss { get; set; } 
    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 
+0

只有'IdentityDbContext'已經繼承'DbContext' - 這是Identity框架的一部分。 – trailmax

相關問題