locked
DropCreateDatabaseIfModelChanges RRS feed

  • Question

  • Entity Framework 4.1 / SqlServerExpress on development machine.

    Please can anyone help with the following?

    Startup from Console App

        static void Main(string[] args)
        {
    
    
          Database.SetInitializer<EntitiesContext>(new EntitiesInitializer());
    
    ......

    Custom Initializer

      public class EntitiesInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext>
      {
        protected override void Seed(EntitiesContext context)
        {
    
          context.CustomEntities.Add(new CustomEntity() { Msg = "I am new", NullableYesNo = null, YesNo = true, TheDate = DateTime.Now });
          context.SaveChanges();
    
          // sample create index
          context.Database.ExecuteSqlCommand("CREATE INDEX IX_CustomEntities_Msg ON dbo.CustomEntities ( Msg )");
        }
    
        public EntitiesInitializer()
        {
          EntitiesContext context = new EntitiesContext();
    
          base.InitializeDatabase(context);
    
        }
      }


    If the schema changes and the database does not exist then the database is created and seeded correctly.

    If the schema changes and the database does exist I get the following error ....

    The model backing the 'EntitiesContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

    Any help will be appreciated.

     

    Friday, August 19, 2011 2:31 PM

Answers

  • Hi Stuart9,

    Welcome!

    You don't need to call >>base.InitializeDatabase(context)

    Here is my code, please feel free to have a try and let me know your feedback:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Database.SetInitializer(new EntitiesInitializer());
          using (var context= new Context())
          {
            context.Database.Initialize(true);
          }
        }
      }
      public class EntitiesInitializer : DropCreateDatabaseIfModelChanges<Context>
      {
        protected override void Seed(Context context)
        {
          context.BT.Add(new B { Id=1});
          context.SaveChanges();
    
        }
      }
      public class A
      {
        public int Id { get; set; }
        public int AId { get; set; }
      }
      public class B
      {
        public B()
        {
          this.As = new List<A>();
        }
        public int Id { get; set; }
        public string Name { get; set; }// Add the Property, you can delete from model change
        public ICollection<A> As
        {
          get;
          set;
        }
      }
      public class Context:DbContext
      {
        public DbSet<A> AT { get; set; }
        public DbSet<B> BT { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          modelBuilder.Entity<B>().HasMany(b => b.As).WithOptional().HasForeignKey(a => a.AId);
        }
      }
      
    }
    
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stuart9 Monday, August 22, 2011 7:24 AM
    • Unmarked as answer by Stuart9 Monday, August 22, 2011 7:25 AM
    • Marked as answer by Stuart9 Monday, August 22, 2011 7:27 AM
    Saturday, August 20, 2011 7:46 AM

All replies

  • Hi Stuart9,

    Welcome!

    You don't need to call >>base.InitializeDatabase(context)

    Here is my code, please feel free to have a try and let me know your feedback:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Database.SetInitializer(new EntitiesInitializer());
          using (var context= new Context())
          {
            context.Database.Initialize(true);
          }
        }
      }
      public class EntitiesInitializer : DropCreateDatabaseIfModelChanges<Context>
      {
        protected override void Seed(Context context)
        {
          context.BT.Add(new B { Id=1});
          context.SaveChanges();
    
        }
      }
      public class A
      {
        public int Id { get; set; }
        public int AId { get; set; }
      }
      public class B
      {
        public B()
        {
          this.As = new List<A>();
        }
        public int Id { get; set; }
        public string Name { get; set; }// Add the Property, you can delete from model change
        public ICollection<A> As
        {
          get;
          set;
        }
      }
      public class Context:DbContext
      {
        public DbSet<A> AT { get; set; }
        public DbSet<B> BT { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          modelBuilder.Entity<B>().HasMany(b => b.As).WithOptional().HasForeignKey(a => a.AId);
        }
      }
      
    }
    
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stuart9 Monday, August 22, 2011 7:24 AM
    • Unmarked as answer by Stuart9 Monday, August 22, 2011 7:25 AM
    • Marked as answer by Stuart9 Monday, August 22, 2011 7:27 AM
    Saturday, August 20, 2011 7:46 AM
  • Thank you Alan

    Following your code sample it now works.

    Monday, August 22, 2011 7:27 AM