我在 Entity Framework 6 中编写了以下自定义约定:
public class NonUnicodeConvention : Convention
{
public NonUnicodeConvention()
{
this.Properties<string>()
.Configure(property => property.IsUnicode(false));
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configurations
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
// Conventions
modelBuilder.Conventions.Add(new NonUnicodeConvention());
}
当我在不进行任何配置的情况下添加字符串属性时,创建的迁移如下所示:
CreateTable(
"dbo.SomeTable",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name= c.String(unicode: false)
})
.PrimaryKey(t => t.Id);
这正是我想要的......但似乎一旦我在特定字段上使用 HasMaxLength 它不再使其成为 unicode:
Property(p => p.Name).HasMaxLength(128);
CreateTable(
"dbo.SomeTable",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name= c.String(maxLength: 128)
})
.PrimaryKey(t => t.Id);
在这种情况下,似乎有些东西正在覆盖 unicode 约定。我想要 unicode 约定,所以我不必在每种情况下都指定它,但是每当我使用 HasMaxLength 时,它似乎都会被覆盖。难道我做错了什么?
如果我使用任何其他特定于字符串的配置(例如 IsFixedLength 或 IsRequired),我不应该认为它不会更改 unicode 属性。