entity-framework - EF 代码优先迁移 : SqlCeException altering NTEXT column

标签 entity-framework sql-server-ce entity-framework-5

我的应用程序在 Sql CE 数据库之上使用 Entity Framework 5.0 代码优先。到目前为止,我们已经使用自动迁移来管理实体映射更改。但是,我现在有一个更改,我需要为其创建自定义迁移以确保在更新期间不会丢失任何数据。我对实体进行了更改,并使用了为我生成 Up() 和 Down() 方法的 Add-Migration 命令。我自定义了 Up() 方法来插入我的自定义 sql 来保存数据,并测试了我的应用程序。

当我运行应用程序时,我收到错误:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

好吧,我不明白这一点,因为我的所有更改都在执行的 Up() 方法中进行了详细说明。 所以我重新打开自动迁移,看看会发生什么。现在我收到此错误:

"Cannot alter column of type NTEXT or IMAGE [ Column Name = LastName ]"

这个错误来自一个表/实体,它甚至没有被我的更改所触及。现有数据库已将该字符串映射到 nvarchar(4000)。如果我在收到此异常后检查数据库,我会发现列已更改为 ntext。英孚在做什么?为什么要触摸未更改的表?我如何才能获得有关这里发生的事情的更多信息?

更新: 作为一种变通方法,我尝试使用这样的数据注释来标记实体中的每个字符串类型:

[Column(TypeName = "ntext")]
public virtual string LastName
{
    get;
    set;
}

现在我所有的字符串都在数据库中使用 ntext。这会在执行查询时导致进一步的异常:

The ntext and image data types cannot be used in WHERE, HAVING, GROUP BY, ON, or IN clauses, except when these data types are used with the LIKE or IS NULL predicates.

总结一下:

  1. 关闭自动迁移会导致 EF 检测到虚拟更改并引发异常
  2. 结合自定义迁移打开自动迁移会导致所有现有字符串映射到 ntext
  3. 无法查询映射到 ntext 的字符串,实际上使它们在我的应用程序中无用

最佳答案

对我来说,对 Up 方法的修改就成功了。

SerialNumber = c.String(maxLength: 99)

被应用而不是

SerialNumber = c.String()

关于entity-framework - EF 代码优先迁移 : SqlCeException altering NTEXT column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589104/

相关文章:

c# - Entity Framework 5 执行全表扫描

asp.net-mvc - 将 1 个数据库用于帐户信息和内容是否正确

.net - LocalDatabase-短期还是长期连接?

c# - 使用 Entity Framework 4 将大量行插入到 SQL CE 4.0(性能问题)

entity-framework-5 - 带有存储库的 Breeze

asp.net - 将 Entity Framework 与大型数据库(600+ 实体)一起使用时内存不足

c# - EF5 一对多关系

c# - Entity Framework : Server did not respond within the specified time out interval

entity-framework - 在 AutoMapper 8.0 中缺少 ResolveUsing

asp.net-mvc - 使用 EF4 转换为日期时间时发生溢出