Entity framework 如何创建自定义的可重用DBContext
三类:Entity framework 如何创建自定义的可重用DBContext,entity-framework,code-first,Entity Framework,Code First,三类: CustomDbContext 可重用上下文 DbContext 关系: 可重用上下文:DbContext CustomDbContext:可重用上下文 代码: 然后我尝试像这样重用ReusableContext: public class CustomDbContext : ReusableDbContext { public CustomDbContext() : base() { Database.SetInitialize
- CustomDbContext
- 可重用上下文
- DbContext
- 可重用上下文:DbContext
- CustomDbContext:可重用上下文
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
}
}
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
- 调用CustomMigrationConfiguration的种子方法并确定
- 但是,ReusableMigrationConfiguration的种子方法并没有被调用-----怎么了
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
}
}
问题解决了,这是解决办法 类别:
- CustomDbContext、CustomMigrationConfiguration
- ReusableContext,ReusableMigrationConfiguration<TContext>
- DbContext
- 可重用上下文:DbContext
- 可重用迁移配置<TContext>DbMigrationsConfiguration<TContext> 其中TContext:ReusableContext
- CustomDbContext:可重用上下文
- CustomMigrationConfiguration:ReusableMigrationConfiguration<CustomDbContext>
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
}
}
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
}
}
项目运行后:
- 创建可重用表和自定义表李>
- 调用CustomMigrationConfiguration的种子方法并确定李>
- 调用ReusableMigrationConfiguration的种子方法,也可以
现在一切似乎都很好:)@Eranga@SONOFGOD我在这里看不到任何区别,请注意CustomDbContext类是从ReusableDbContext而不是DbContext扩展的。我打算做的是尝试重用ReusableDbContext(创建公共表的代码优先方法)和ReusableMigrationConfiguration的逻辑(用于创建视图、存储过程等的种子逻辑),可能将它们放在一个单独的项目中。在CustomDbContext中,您可以定义自己的表…通过从ReusableDbContext扩展,您可以将工作保存到创建这些公共表和其他公共数据库对象。我的问题是,ReusableMigrationConfiguration的种子方法从未调用,因此视图、存储过程或不可创建。
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
}
}