如何创建自定义可重用DbContext

时间:2012-03-26 16:10:20

标签: entity-framework code-first

三课:

  • 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:

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
    }
}
运行项目后

  • 创建了ReusableTable和CustomTable
  • 调用CustomMigrationsConfiguration的种子方法,然后单击“确定”
  • 但是,没有调用ReusableMigrationsConfiguration的种子方法--------有什么问题?

2 个答案:

答案 0 :(得分:1)

问题解决了,这是解决方案。

班级:

  • CustomDbContext,CustomMigrationsConfiguration
  • ReusableContext,ReusableMigrationsConfiguration
  • 的DbContext

关联:

  • ReusableContext:DbContext
  • 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的种子方法,然后单击“确定”;
  • ReusableMigrations的种子方法调用配置也可以。

现在一切似乎都没事了。)

答案 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
    }
}