ClickOnce更新和实体框架代码优先

时间:2013-11-20 22:19:59

标签: c# entity-framework clickonce

如果我构建一个应用程序并让代码首先找出放置数据库的位置并且用户通过应用程序插入数据,那么一旦更新,该数据是否会丢失?如果是这样,我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:3)

使用自动迁移时,不需要“丢失”任何数据。 迁移配置类应声明不允许数据丢失 在处理导致数据丢失的更改时,您需要构建自定义脚本/或调整生成的脚本。

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

    }
  

如何进行迁移。

..这实际上是个大问题。

EF6 Migrations - new features and options

Great info on Migrations when working in teams.
这涵盖了您可能认识到的许多场景,从而帮助您了解最适合您的方法。