0
- 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的種子方法不被稱爲--------出了什麼問題?
@Eranga @ SONOFGOD我看不出這裏有什麼區別,請注意類CustomDbContext從ReusableDbContext延伸,而不是的DbContext。 – Dean 2012-03-27 06:36:54
我打算做的是試圖重用ReusableDbContext(代碼第一種方法創建公共表)的邏輯和ReusableMigrationsConfiguration(種子邏輯創建視圖,存儲過程等),也許把它們放在一個單獨的項目中。在CustomDbContext中,您可以定義自己的表...通過從ReusableDbContext擴展,您可以將工作保存爲創建這些常用表和其他常用數據庫對象。我的問題是從未調用ReusableMigrationsConfiguration的Seed方法,因此無法創建視圖Stored Procedures ...。 – Dean 2012-03-27 07:15:21