情况:
我有一个基类 Lookup ,如下所示:
public abstract class Lookup : DeactivatableDomainModel {
[Required]
[Key]
public int ID { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int DisplayOrder { get; set; }
}
我还创建了一个属性 IsLookup :
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class IsLookup : Attribute {
public int DescriptionLength { get; set; }
public int CodeLength { get; set; }
public IsLookup(int DescriptionLength, int CodeLength = 0) {
this.DescriptionLength = DescriptionLength;
this.CodeLength = CodeLength;
}
}
目标是能够创建以下声明:
[IsLookup(40)]
public class TestCategory : Lookup { }
...并使用OnModelCreating
将属性描述的MaxLength
设置为40。
我已经能够对看起来应该工作的东西进行编码,并且add-migration
运行得很好,但是生成的迁移没有设置maxlength:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Properties()
.Where(p => p.Name == "Description" && p.DeclaringType.GetCustomAttributes(false).OfType<IsLookup>().FirstOrDefault() != null)
.Configure(
c => c.HasMaxLength(((IsLookup)c.ClrPropertyInfo.DeclaringType.GetCustomAttributes(typeof(IsLookup), false).FirstOrDefault()).DescriptionLength)
);
}
结果是:
CreateTable(
"Lookups.TestCategories",
c => new {
ID = c.Int(nullable: false, identity: true),
Description = c.String(nullable: false),
DisplayOrder = c.Int(nullable: false),
Active = c.Boolean(nullable: false),
RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
})
.PrimaryKey(t => t.ID);
所以,问题是......为什么描述在迁移代码中没有设置长度?这甚至可能吗?
<gripe>
如果有办法调试add-migration
,这会更容易。</gripe>
答案 0 :(得分:0)
我很确定DeclaringType
将为您提供声明属性的类型,在您的示例中,Lookup
类是声明Description
属性的位置。由于Lookup
没有IsLookup
属性,因此不会设置任何内容。首先尝试查看已注册的类型,然后在找到后设置“描述”列:
modelBuilder.Types()
.Where(t => t.IsSubclassOf(typeof(Lookup)))
.Having(x => x.GetCustomAttributes(false).OfType<IsLookup>().FirstOrDefault())
.Configure((config, att) => {
config.Property("Description").HasMaxLength(att.DescriptionLength);
});