Bill Greer
Bill Greer

Reputation: 3156

ClickOnce Updates and the Entity Framework Code First

If I build an application and let code first figure out where to put the database and the user inserts data via the application, will that data get lost on a click once update ? If so, how do I approach this problem ?

Thanks

Upvotes: 0

Views: 674

Answers (1)

phil soady
phil soady

Reputation: 11328

No there is No need to "lose" any data when using automatic migrations. You migration config class should state no data loss allowed You will need to build custom scripts/ or tweak the generated scripts when dealing with changes that result in data loss.

public override void MigrateDb() {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MYDbContext, MYSECIALMigrationConfiguration>());
       // Context = GetDefaultContext();  // check if a new context is really needed here 
        Context.Database.Initialize(true);
    }

public class MYSPECIALMigrationConfiguration : MYBaseMigrationConfiguration<MYDbContext>{  }


 public abstract class MYBaseMigrationConfiguration<TContext> : DbMigrationsConfiguration<TContext> 
    where TContext  : DbContext{

    protected  MYBaseMigrationConfiguration() {
        AutomaticMigrationsEnabled = true;  // you can still chnage this later if you do so before triggering Update
        AutomaticMigrationDataLossAllowed = true; // you can still chnage this later if you do so before triggering Update

    }

how to approach migrations.

.. Thats actual a big question.

EF6 Migrations - new features and options

Great info on Migrations when working in teams.
This covers many scenarios you may recognise and thus help you understand what approach suits you best.

Upvotes: 3

Related Questions