5

I have this db configuration

public class AppDbContext : DbContext 
    {

         public AppDbContext(string connectionStringOrName)
            : base(connectionStringOrName)
        {
            Database.SetInitializer(new AppDbInitializer());
        }

         public AppDbContext()
             : this("name=AppDbContext")
        {

        }

         public DbSet<User> Users { get; set; }
         public DbSet<Log> Logs { get; set; }
    }

and I have this migration configuration

public class AppDbInitializer : MigrateDatabaseToLatestVersion<AppDbContext,AppDbMigrationConfiguration>
{
}


  public class AppDbMigrationConfiguration : DbMigrationsConfiguration<AppDbContext>
    { 
        public AppDbMigrationConfiguration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(AppDbContext context)
        {
            if (context.Users.Any()) return;

            AddAdmin(context, "Admin", "admin@test.com");
        }
   }

And I added another field to Log entity.

Can Entity Framework automatically detect and apply changes?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
cihancoskun
  • 591
  • 5
  • 7
  • What should this do? What does it do? – CodeCaster Jan 06 '15 at 11:40
  • What have you tried yourselft do you get some exception or what? Take a look at the following link about setting up EF Migrations Code first http://msdn.microsoft.com/nl-nl/data/jj591621.aspx – Jordy van Eijk Jan 06 '15 at 11:46
  • 1
    Entity Framework does not do anything. That's the problem. I know I can manuel migration with Up() and Down() functions. But I want to AutoMigration. By the way I forgot to show you how I migrateDatabase. public class AppDbInitializer : MigrateDatabaseToLatestVersion { } – cihancoskun Jan 06 '15 at 12:27
  • While your changes to Model will not lead to DATALOSS, EF will not do anything... – Roman Feb 22 '19 at 12:22

1 Answers1

0

If Automatic Migrations are enabled, it should auto detect any small changes in the model.

But for larger changes, eg addition of new entity, I have seen to manually apply migration, which you can do with "Add-Migration" and then running "Update-Database"

Bhavjot
  • 544
  • 5
  • 16
  • When does the automatic migration take place **exactly**? If I change *int* to *int?* and save the file with the class containing that property? Or do we still have to run *Update-Database* but without the requirement of calling *Add-Migration xxx* first? – Konrad Viltersten Aug 06 '16 at 10:28
  • Yes, you are right. for small changes, and if Automatic Migrations is enabled, calling Update-Database will automatically add a migration and update database. – Bhavjot Aug 08 '16 at 03:52