实体框架告诉我,背景的模式已经改变了 [英] Entity Framework telling me the model backing the context has changed

查看:115
本文介绍了实体框架告诉我,背景的模式已经改变了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,实体框架代码第一次迁移。我一直在使用EF和代码首个迁移项目几个月,事情是正常的。我最近创建了一个新的迁移,当运行更新数据库我的数据库的恢复备份时,我收到这个错误:

I have a weird problem with Entity Framework code first migrations. I've been using EF and code first migrations on a project for months now and things are working fine. I recently created a new migration and when running Update-Database a restored backup of my database I get this error:


自创建
数据库以来,支持上下文的模型已更改。考虑使用代码优先迁移更新数据库

The model backing the context has changed since the database was created. Consider using Code First Migrations to update the database

迁移执行类似以下操作:

The migration does something like the following:

public override void Up()
{
    using (SomeDbContext ctx = new SomeDbContext())
    {
        //loop through table and update rows
        foreach (SomeTable table in ctx.SomeTables)
            table.SomeField = DoSomeCalculation(table.SomeField);

        ctx.SaveChanges();
    }
}

我没有使用Sql()函数,因为DoSomeCalculation必须在C#代码中完成。

I'm not using the Sql() function because DoSomeCalculation must be done in C# code.

通常当我得到这样的东西意味着我以某种方式更新了我的模型,忘记了创建一个迁移。但是这次不是这样。奇怪的是,这个错误甚至不会在几天前创建的迁移中发生,并且工作正常。

Usually when I get something like this is means that I have updated my model somehow and forgot to create a migration. However that's not the case this time. The weird thing is that the error isn't even occurring on a migration that I created a few days ago and had been working fine.

我看了一个quite 几个粒子关于,而他们也是这样的所有这一切似乎都称为

I looked a quite a few articles about this and they all seems to say call

Database.SetInitializer<MyContext>(null);

这样做似乎有效,但我的理解(基于这篇文章)认为,这样做会消除EF的能力确定数据库和模型何时不同步。我不想这样做我只是想知道为什么它认为他们突然间不同步。

Doing that does seem to work, but my understanding (based on this article) is that doing that will remove EF's ability to determine when the database and model are out of sync. I don't want to do that. I just want to know why it thinks they are out of sync all of a sudden.

我也尝试运行Add-Migration,看看它是如何改变的模型,但它不会让我这样做,说明我有待处理的迁移运行。好的,抓住22,微软。

I also tried running Add-Migration just to see if what it thought changed about the model but it won't let me do that stating that I have pending migrations to run. Nice catch 22, Microsoft.

任何猜测这里发生了什么?

Any guesses as to what's going on here?

我想知道也许事实上面列出的迁移是使用EntityFramework的问题。似乎也许,因为它不是最新的迁移,当EF收到它试图创建一个SomeDbContext对象,它检查数据库(由于我们正在运行迁移中,它不完全是最新的)对我的当前代码模型然后抛出上下文已更改错误。

I'm wondering if maybe the fact that migration listed above is using EntityFramework is the problem. Seems like maybe since it's not the latest migration anymore, when EF gets to it tries to create a SomeDbContext object it checks the database (which is not fully up to date yet since we're in the middle of running migrations) against my current code model and then throws the "context has changed" error.

推荐答案

我尝试用常规ADO.NET代码替换EntityFramework代码,它似乎工作。这是它的样子:

I tried replacing the EntityFramework code with regular ADO.NET code and it seems to work. Here is what it looks like:

public override void Up()
{
    Dictionary<long, string> idToNewVal = new Dictionary<long, string>();

    using (SqlConnection conn = new SqlConnection("..."))
    {
        conn.Open();

        using (SqlCommand cmd = new SqlCommand("SELECT SomeID, SomeField FROM SomeTable", conn))
        {
            SqlDataReader reader = cmd.ExecuteReader();

            //loop through all fields, calculating the new value and storing it with the row ID
            while (reader.Read())
            {
                long id = Convert.ToInt64(reader["SomeID"]);
                string initialValue = Convert.ToString(reader["SomeField"]);
                idToNewVal[id] = DoSomeCalculation(initialValue);
            }
        }
    }

    //update each row with the new value
    foreach (long id in idToNewVal.Keys)
    {
        string newVal = idToNewVal[id];
        Sql(string.Format("UPDATE SomeTable SET SomeField = '{0}' WHERE SomeID = {1}", newVal, id));
    }
}

这篇关于实体框架告诉我,背景的模式已经改变了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆