【问题标题】:Entity code first migrations and deleting databases实体代码先迁移和删除数据库
【发布时间】:2014-05-27 19:45:07
【问题描述】:

我们正在为一个项目使用 EF Code First 和迁移。我们正在将我们的迁移提交给源,一切都很好。但是,如果有人删除了他们的数据库,或者我们在项目中有一个新人,数据库将抛出错误,因为它正在尝试运行迁移。有没有办法让它在数据库不存在时忽略迁移?我似乎找不到任何关于此的信息。

【问题讨论】:

    标签: c# wpf entity-framework


    【解决方案1】:

    我会看看您如何使用实体框架中的 DbMigrationsConfiguration。你的全局 asx 文件中可能需要这样的东西:

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<YourContext, YourConfiguration>());
    

    然后在你的迁移配置文件中你可能需要这样的东西:

        internal sealed class YourConfiguration : DbMigrationsConfiguration<YourContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }...
    

    如果没有更多信息和代码示例,我只能为您指明正确的方向。

    【讨论】:

    • 好的,我的配置文件中有AutomaticMigrationsEnabled = true,我的AppBootstrapper.cs 文件中有SetInitilizer(这不是aspx,它是一个WPF 项目)。由于错误,它仍然无法创建多个表。
    • System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "dbo.Plan" because it does not exist or you do not have permissions. 我收到该错误是因为该表尚不存在。
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多