【发布时间】: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
生成的迁移类默认为最大长度为4000 的string 列。
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")
结果表已正确创建。
【问题讨论】:
-
如前所述,这些属性对创建迁移没有任何影响。
-
我可以重现,我会在 Codeplex 提交错误。
标签: c# entity-framework sql-server-ce entity-framework-6