实体框架复杂类型的列命名约定

时间:2015-10-20 13:33:08

标签: c# entity-framework entity-framework-6

使用复杂类型,默认列命名约定使用下划线。这意味着以这种方式定义类型:

[ColmplexType]
public class Contact
{
    string Email {get;set;}
    string Post {get;set;}
}

public EntityN
{
    //...
    public Contact Contact {get;set;}
}

我们将获得以这种方式命名的列

Contact_Email nvarchar(max)
Contact_Post nvarchar(max)

我们当然可以使用ColumnAttribute或Context.Properties映射分别配置每个列名,但我们是否有可能创建命名约定,因此为currnet类型配置所有名称一次?

对于某些复杂类型,我宁愿不提及属性名称(" Contact"),其他人使用CammelCase连接名称和属性,并且永远不会使用undersore。

讨论:

可行(创建特定表的配置信息)

    public class CustomComplexTypeAttributeConvention : ComplexTypeAttributeConvention
    {
        public override void Apply(ConventionTypeConfiguration configuration, ComplexTypeAttribute attribute)
        {
            Properties().Where(pi => pi.DeclaringType == typeof(Contact))
               .Configure(p => p.HasColumnName(p.ClrPropertyInfo.Name)
            );
            base.Apply(configuration, attribute);
        }
    }

和OnModelCreating

modelBuilder.Conventions.AddBefore<ComplexTypeAttributeConvention>(new CustomComplexTypeAttributeConvention());

它有效,但我不确定它是一种正确的编码方式: 1)&#34; AddBefore&#34;按预期工作(我不想删除默认行为,只想覆盖一个案例的默认行为)? 2)放置&#34;自定义代码&#34;的最佳选择在哪里?应用方法或构造函数。

ComplexTypeAttributeConvention的断点和反汇编带来了一个我们不会覆盖&#34;默认&#34;命名约定但使用&#34;循环&#34;通过&#34;所有类型的所有属性&#34;。

这看起来像是最坚固的解决方案,但它仍然是一个&#34; hack&#34; (它不会覆盖默认&#34;下划线&#34;约定,但会模拟&#34; ColumnAttribute&#34;的呈现:

    public class BriefNameForComplexTypeConvention<T> : Convention
    {
        public BriefNameForComplexTypeConvention()
        {
            Properties().Where(pi => pi.DeclaringType == typeof(T))
               .Configure(p => p.HasColumnName(p.ClrPropertyInfo.Name)
            );
        }
    }
    // ...
    modelBuilder.Conventions.Add(new BriefNameForComplexTypeConvention<Contact>());

1 个答案:

答案 0 :(得分:3)

我之前从未这样做过,但值得尝试ComplexTypeAttributeConvention,您可以删除默认值并将自定义值添加到DbModelBuilder.Conventions

public class CustomComplexTypeAttributeConvention : ComplexTypeAttributeConvention {
    public CustomComplexTypeAttributeConvention(){
       Properties().Configure(p => p.HasColumnName(p.ClrPropertyInfo.Name));
    }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder){
   modelBuilder.Conventions.Remove<ComplexTypeAttributeConvention>();
   modelBuilder.Conventions.Add(new CustomComplexTypeAttributeConvention());
   //...
}