简体   繁体   English

为EF6中的所有属性创建自定义属性映射

[英]Create custom property mapping for all properties in EF6

Model: 模型:

Entity class as the Id is shared between all entities 实体类,因为ID在所有实体之间共享

public abstract class Entity
{
    public int Id { get; set; }
}

And the Student class, which inherits the Entity class 还有Student类,它继承了Entity类

public class Student : Entity
{
    public string LastName { get; set; }

    public string Forenames { get; set; }

    public DateTime EnrolmentDate { get; set; }

    public virtual ICollection<Enrolment> Enrolments { get; set; }
}

Table columns have the naming convention STUDENT_ID, LAST_NAME, FORENAMES etc. All tables will have the ID column of [NAME]_ID 表格列的命名约定为STUDENT_ID,LAST_NAME,FORENAMES等。所有表格的ID列均为[NAME] _ID

How do I create a custom mapping for all properties in all entity classes? 如何为所有实体类中的所有属性创建自定义映射? The code referenced here does not work as the .Entities() method does not exist any more. 此处引用的代码不起作用,因为.Entities()方法不再存在。

Override the OnModelCreating method on the context class, and then you can set a custom name for the ID column for each entity like this: 覆盖上下文类的OnModelCreating方法,然后可以为每个实体的ID列设置自定义名称,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>().Property(x => x.Id).HasColumnName("STUDENT_ID");
    modelBuilder.Entity<Teacher>().Property(x => x.Id).HasColumnName("TEACHER_ID");
}

You can use the column attribute to have the custom column name mapping: 您可以使用column属性具有自定义列名映射:

public class Student
{
  [Column("STUDENT_ID")]
  public int Id { get; set; }

  //...
}

I am unable to mark both answers as correct (which they are), but the idea I was trying to get to was to set all column names automatically map from ColumnName to COLUMN_NAME without having to specify the column name manually for every property. 我无法将两个答案都标记为正确(它们都是正确的),但是我试图达到的目的是将所有列名自动设置为从ColumnName映射到COLUMN_NAME,而不必手动为每个属性指定列名。

I found the answer from Loop over entity column mappings to transform column name which works for every column except the ID column, so I just set these manually with the code that Yacoub Massad suggested. 我从“ 循环遍历实体列”映射中找到答案, 可以转换列名 ,该列名适用于除ID列之外的所有列,因此我只是使用Yacoub Massad建议的代码手动设置了它们。

So: 所以:

// Make all properties UPPER_CASE
modelBuilder.Properties()
            .Configure(x => x.HasColumnName(Regex.Replace(x.ClrPropertyInfo.Name, "(?<=.)(?=[A-Z])", "_").ToUpper()));

// Set the ID columns to ENTITY_ID
modelBuilder.Entity<Course>()
    .ToTable("COURSE")
    .Property(x => x.Id)
    .HasColumnName("STUDENT_ID");

Setting the [Column("ENTITY_ID")] attribute for every property would be fine if I didn't have that many tables and columns, but as the database grows and there are more and more entity classes added it can be quite repetitive and could be brittle as it's manually named. 如果我没有那么多的表和列,则为每个属性设置[Column("ENTITY_ID")]属性就可以了,但是随着数据库的增长以及添加的实体类越来越多,它可能会很重复并且可以易碎,因为它是手动命名的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM