EF6根据包含类的属性设置属性的MaxLength

时间:2016-06-16 13:42:07

标签: c# entity-framework-6 ef-model-builder

情况:

我有一个基类 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>

1 个答案:

答案 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);
    });