none
Entity Framework Initializer - seed database RRS feed

  • Question

  • I'm using Entity Framework with code first migrations. I want to seed my database after creation(I'm using creatIfNotExist). Is it possible to combine migrations with initializer that inherit from creatIfNotExist?

    my code:

    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.
            }

    Tuesday, January 26, 2016 4:35 PM

All replies