2012-03-26 95 views
0

三類:如何創建自定義ReusableDbContext

  • CustomDbContext
  • ReusableContext
  • 的DbContext

關係:

  • ReusableContext:的DbContext
  • CustomDbContext:ReusableContext

代碼:

public class ReusableDbContext : DbContext 
{ 
    public ReusableDbContext() 
     : base("name = myConnectionString") 
    { 
     Database.SetInitializer<ReusableDbContext>(new 
MigrateDatabaseToLatestVersion<ReusableDbContext, ReusableMigrationsConfiguration>()); 
    } 

    public DbSet<ReusableTable> ReusableTables{ get; set; } 
} 

public class ReusableMigrationsConfiguration : DbMigrationsConfiguration<ReusableDbContext> 
{ 
    public ReusableMigrationsConfiguration() 
     : base() 
    { 
     AutomaticMigrationsEnabled = true; 
     AutomaticMigrationDataLossAllowed = true; 
    } 

    protected override void Seed(ReusableDbContext context) 
    { 
     //ReusableMigrationsConfiguration Seed Logic here 
     // this Seed method is never called ----------- The Problem here! 
    } 
} 

然後我嘗試重用ReusableContext這樣的:

  • ReusableTable和CustomTable:項目運行後

    public class CustomDbContext : ReusableDbContext 
    { 
        public CustomDbContext() 
         : base() 
        { 
         Database.SetInitializer<CustomDbContext>(new 
    MigrateDatabaseToLatestVersion<CustomDbContext, CustomMigrationsConfiguration>()); 
        } 
    
        public DbSet<CustomTable> CustomTables{ get; set; } 
    } 
    
    public class CustomMigrationsConfiguration : DbMigrationsConfiguration<CustomDbContext> 
    { 
        public CustomMigrationsConfiguration() 
         : base() 
        { 
         AutomaticMigrationsEnabled = true; 
         AutomaticMigrationDataLossAllowed = true; 
        } 
    
        protected override void Seed(CustomDbContext context) 
        { 
         //CustomMigrationsConfiguration Seed Logic 
         // this Seed method is called, no problem here 
        } 
    } 
    

    被創建

  • CustomMigrationsConfiguration的種子方法被調用並確定
  • 但是,ReusableMigrationsConfiguration的種子方法不被稱爲--------出了什麼問題?

回答

1

問題解決了,這裏是解決方案。

類別:

  • CustomDbContext,CustomMigrationsConfiguration
  • ReusableContext,ReusableMigrationsConfiguration
  • 的DbContext

關聯:

  • ReusableContext:DbConte XT
  • ReusableMigrationsConfiguration :DbMigrationsConfiguration 其中TContext:ReusableContext
  • CustomDbContext:ReusableContext
  • CustomMigrationsConfiguration:ReusableMigrationsConfiguration

代碼:

public class ReusableDbContext : DbContext 
{ 
    public ReusableDbContext() 
     : base("name = myConnectionString") 
    { 
     Database.SetInitializer<ReusableDbContext>(new 
MigrateDatabaseToLatestVersion<ReusableDbContext, ReusableMigrationsConfiguration>()); 
    } 

    public DbSet<ReusableTable> ReusableTables{ get; set; } 
} 

public class ReusableMigrationsConfiguration<TContext> : DbMigrationsConfiguration<TContext> where TContext : ReusableDbContext 
{ 
    public ReusableMigrationsConfiguration() 
     : base() 
    { 
     AutomaticMigrationsEnabled = true; 
     AutomaticMigrationDataLossAllowed = true; 
    } 

    protected override void Seed(TContext context) 
    { 
     //ReusableMigrationsConfiguration Seed Logic here 
     // is called and OK now. 
    } 
} 

現在我們可以像這樣重用ReusableContext:

public class CustomDbContext : ReusableDbContext 
{ 
    public CustomDbContext() 
     : base() 
    { 
     Database.SetInitializer<CustomDbContext>(new 
MigrateDatabaseToLatestVersion<CustomDbContext, CustomMigrationsConfiguration>()); 
    } 

    public DbSet<CustomTable> CustomTables{ get; set; } 
} 

public class CustomMigrationsConfiguration : ReusableMigrationsConfiguration<CustomDbContext> 
{ 
    public CustomMigrationsConfiguration() 
     : base() 
    { 
     AutomaticMigrationsEnabled = true; 
     AutomaticMigrationDataLossAllowed = true; 
    } 

    protected override void Seed(CustomDbContext context) 
    { 
     base.Seed(context); 



     //CustomMigrationsConfiguration Seed Logic 
     // this Seed method is called and OK 
    } 
} 

運行的項目後:

  • ReusableTable和CustomTable創建;
  • CustomMigrationsConfiguration的種子方法被調用並確定;
  • ReusableMigrationsConfiguration的Seed方法也被調用,也可以。

所有事情似乎現在要罰款:)

0
public class CustomDbContext : **ReusableDbContext** 
{ 
    public CustomDbContext() 
     : base() 
    { 
     Database.SetInitializer<CustomDbContext>(new 
MigrateDatabaseToLatestVersion<CustomDbContext, CustomMigrationsConfiguration>()); 
    } 

    public DbSet<CustomTable> CustomTables{ get; set; } 
} 

做到這一點,將工作

public class CustomMigrationsConfiguration : DbMigrationsConfiguration<CustomDbContext> 
{ 
    public ReusableMigrationsConfiguration() 
     : base() 
    { 
     AutomaticMigrationsEnabled = true; 
     AutomaticMigrationDataLossAllowed = true; 
    } 

    protected override void Seed(CustomDbContext context) 
    { 
     //CustomMigrationsConfiguration Seed Logic 
     // this Seed method is called, no problem here 
    } 
} 
+0

@Eranga @ SONOFGOD我看不出這裏有什麼區別,請注意類CustomDbContext從ReusableDbContext延伸,而不是的DbContext。 – Dean 2012-03-27 06:36:54

+0

我打算做的是試圖重用ReusableDbContext(代碼第一種方法創建公共表)的邏輯和ReusableMigrationsConfiguration(種子邏輯創建視圖,存儲過程等),也許把它們放在一個單獨的項目中。在CustomDbContext中,您可以定義自己的表...通過從ReusableDbContext擴展,您可以將工作保存爲創建這些常用表和其他常用數據庫對象。我的問題是從未調用ReusableMigrationsConfiguration的Seed方法,因此無法創建視圖Stored Procedures ...。 – Dean 2012-03-27 07:15:21