实体框架初始化程序 - 种子数据库

时间:2016-01-26 16:32:11

标签: c# entity-framework visual-studio initialization ef-migrations

我使用Entity Framework进行代码优先迁移。我想在创建后为我的数据库播种(我使用creatIfNotExist)。是否可以将迁移与继承自creatIfNotExist的初始化程序结合使用?

我的代码:

 static class Program
 {
   static void Main()
   {

     using (var db = new myDbContext())
     {
       db.Database.CreateIfNotExists();
     }
   }
}

  public sealed class Configuration : DbMigrationsConfiguration<Core.Models.SpanTesterContainer>
  {


    public Configuration()
    {
    AutomaticMigrationsEnabled = true;

    }

    protected override void Seed(Core.Models.SpanTesterContainer context){}
  } 


  public partial class myDbContext : DbContext
  {
    static SpanTesterContainer()
    {
      Database.SetInitializer<myDbContext>(new myDBInitializer());
    }

    public myDbContext()
    : base("Data Source=" +
           Properties.Settings.Default.ServerName + ";Initial Catalog=" +
           Properties.Settings.Default.DbName + ";Integrated Security=True;MultipleActiveResultSets=True")
    {
   }


public class SpanTesterDBInitializer : CreateDatabaseIfNotExists<SpanTesterContainer>
    {
        protected override void Seed(SpanTesterContainer context)
        {
           //Seed code here.
        }

0 个答案:

没有答案