【问题标题】:Generate ntext/nvarchar column when using Entity Framework Code First and a DBMigration使用 Entity Framework Code First 和 DBMigration 时生成 ntext/nvarchar 列
【发布时间】:2014-03-15 06:53:34
【问题描述】:

我想使用 Fluent API 或数据注释能够自动为字符串列生成数据库迁移类作为 ntext 而不是固定长度的字符串。我该怎么做?

当使用 Entity Framework v6、Code First 和 SQL Server Compact 4 时,给定一个简单的 C# 类,例如:

public class GameFile
{
    public string Id { get; set; }        
    public string FileData { get; set; }
    public int FileSize { get; set; }
}

然后,在添加新迁移时:

add-migration first

生成的迁移类默认为最大长度为4000string 列。

CreateTable(
    "dbo.GameFiles",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 4000),
            FileData = c.String(maxLength: 4000),
            FileSize = c.Int()
        })
    .PrimaryKey(t => t.Id);

当通过update-database 更新数据库时,该表将有一个名为FileData 类型为nvarchar(4000) 的列。

我尝试将MaxLength 和/或Column 属性添加到FileData 属性,但生成的迁移代码没有变化。

[MaxLength, Column(TypeName = "ntext")]
public string FileData { get; set; }

我知道我是否手动编辑迁移代码以显式声明storeType

FileData = c.String(storeType: "ntext")

结果表已正确创建。

【问题讨论】:

标签: c# entity-framework sql-server-ce entity-framework-6


【解决方案1】:

似乎该错误已针对 Entity Framefork v6.1.3 修复。

以下定义按预期工作 - 创建 ntext 列。

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

【讨论】:

  • 请注意:“ntext、text 和 image 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改应用程序当前使用它们。请改用 nvarchar(max)、varchar(max) 和 varbinary(max)。来源:docs.microsoft.com/en-us/sql/t-sql/data-types/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多