C# 实体框架中编译前的DbMigration和DbModelBuilder行为
我有这样一个属性:C# 实体框架中编译前的DbMigration和DbModelBuilder行为,c#,asp.net,sql-server,entity-framework,C#,Asp.net,Sql Server,Entity Framework,我有这样一个属性: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IsUnicodeAttribute : Attribute { public bool IsUnicode { set; get; } public IsUnicodeAttribute(bool isUnicode) { this.IsUnicode = isUnicode
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IsUnicodeAttribute : Attribute
{
public bool IsUnicode { set; get; }
public IsUnicodeAttribute(bool isUnicode)
{
this.IsUnicode = isUnicode;
}
}
我在DbModelBuilder上读到:
modelBuilder.Properties<String>()
.Having(p => p.GetCustomAttributes(false).OfType<IsUnicodeAttribute>().FirstOrDefault())
.Configure((config, attr) => config.IsUnicode(attr.IsUnicode));
虽然还没有数据库,但我启用了迁移并添加了第一次迁移。但是,我注意到,在创建的迁移文件中,没有显示那些用属性标记为Unicode的列
是因为我在配置中出错,还是因为在运行时不知道属性的IsUnicode属性的值,还是Unicode是默认值
我使用的是EF 6.1.1。我刚在数据库创建后检查了它,发现实际上NVARCHAR是我在类模型中声明的所有字符串属性的数据类型,而不需要在它们上面指定IsUnicode 这看起来至少是SQL Server提供程序的默认设置 编辑:我使用modelBuilder.Entity.Propertya=>a.Message.isunicodelse,创建的迁移文件实际指定了这一点。结果表明,默认情况下字符串是Unicode的,因此无需在迁移文件中指定该字符串,并且默认情况下它们看起来是NVARCHAR;不是固定大小的