简体   繁体   English

实体框架在运行时应用迁移

[英]Entity Framework apply migrations at runtime

i'm creating a C# WinForms application which uses Entity Framework Code First and it is set to create the database if it doesn't exists. 我正在创建一个使用Entity Framework Code First的C#WinForms应用程序,如果该数据库不存在,它将设置为创建数据库。

Since the app is not distributed with a database, it creates it when it's needed, so i need to find a way to detect which migrations need to be applied for each case when i release a new version of the app. 由于该应用程序未随数据库一起分发,因此它会在需要时创建它,因此我需要找到一种方法来检测发布新版本应用程序时每种情况下需要进行哪些迁移。

How can i detect and apply needed migrations at runtime? 如何在运行时检测和应用所需的迁移?

try this Initializer:System.Data.Entity.MigrateDatabaseToLatestVersion,it will update your database(no delete db,no delete data),just update entity changed. 试试这个Initializer:System.Data.Entity.MigrateDatabaseToLatestVersion,它将更新您的数据库(不删除db,不删除数据),只是更新了实体。

Database.SetInitializer(new MigrateDatabaseToLatestVersion<T, DbMigrationsConfiguration<T>>());
        try
        {
            using (var ctx = new T())
            {
                ctx.Database.Initialize(true);
            }
        }
        catch (Exception e)
        {
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM