三课:
关联:
代码:
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:
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
}
}
运行项目后:
答案 0 :(得分:1)
问题解决了,这是解决方案。
班级:
关联:
代码:
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
}
}
运行项目后:
现在一切似乎都没事了。)
答案 1 :(得分: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
}
}