2013-11-20 25 views
0

如果我構建一個應用程序並讓代碼首先找出將數據庫放到哪裏以及用戶通過應用程序插入數據,那麼一旦更新,點擊就會丟失數據?如果是這樣,我該如何解決這個問題?ClickOnce更新和實體框架代碼優先

感謝

+0

如果您正確設置EF遷移,則在更新過程中不應丟失任何數據。 – SOfanatic

+0

我在開發階段之前曾使用過EF遷移。在我的場景中使用(發佈後)可以嗎? –

+1

是的,如果您要更改代碼,那麼遷移將保持模型和數據庫同步。因爲在部署後想要將'AutomaticMigrationDataLossAllowed'設置爲'false',但它仍然可以工作,所以做出一些更改可能會更困難。看看菲爾的答案。 – SOfanatic

回答

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.
這包括你可能認識,從而幫助您瞭解什麼方法最適合你的許多場景。

相關問題