Entity framework 更改所有实体的标识列的名称

Entity framework 更改所有实体的标识列的名称,entity-framework,entity-framework-6,Entity Framework,Entity Framework 6,我正在创建一个域模型,希望有一个带有“Id”属性的“BaseEntity”类(以及其他一些审计跟踪的东西)。Id属性是主键,我的域模型中的每个实体都将从BaseEntity类继承。非常简单的东西 public class BaseEntity { [Key] public int Id { get; set; } public DateTime LastUpdate { get; set; } public string LastUpdateBy { get;

我正在创建一个域模型,希望有一个带有“Id”属性的“BaseEntity”类(以及其他一些审计跟踪的东西)。Id属性是主键,我的域模型中的每个实体都将从BaseEntity类继承。非常简单的东西

public class BaseEntity
{
    [Key]
    public int Id { get; set; }

    public DateTime LastUpdate { get; set; }
    public string LastUpdateBy { get; set; }
}
public class Location : BaseEntity
{
    [Required]
    public string Name { get; set; }

    public string Description { get; set; }
}
使用上面的示例,我想将“Id”字段映射到“LocationId”列。我知道我可以使用modelBuilder为每个实体显式地执行以下操作:

modelBuilder.Entity<Location>().Property(s => s.Id).HasColumnName("LocationId");
public void Apply(PropertyInfo propertyInfo, Func<PrimitivePropertyConfiguration> configuration)
{
    if (propertyInfo.Name == "Id")
    {
        configuration().ColumnName = propertyInfo.ReflectedType.Name + "Id";
    }
}
modelBuilder.Entity().Property(s=>s.Id).HasColumnName(“LocationId”);
但我想对我的领域模型中的每个实体都这样做,这会很难看

我尝试了下面的思考,但没有任何运气。无论出于何种原因,编译器“无法解析符号类型”:

foreach(GetTypesInNamespace(Assembly.Load(“Domain.Model”),“Domain.Model”)中的变量类型)
{
modelBuilder.Entity()属性(x=>x.Id)。。。。。
}

有没有办法定义一个约定来覆盖默认的PrimaryKey约定,将我的“Id”属性映射到数据库中的“ClassNameId”属性?我使用的是实体框架6。

如果不使用自定义约定,则是动态方法的开始

 modelBuilder.Entity<Location>().Property(s => s.Id).HasColumnName("LocationId");
以下是我用于此类解决方案的扩展

    // DBSet Types is the Generic Types POCO name  used for a DBSet
    public static List<string> GetModelTypes(this DbContext context) {
        var propList = context.GetType().GetProperties();
        return GetDbSetTypes(propList);
    }

    // DBSet Types POCO types as IEnumerable List
    public static IEnumerable<Type> GetDbSetPropertyList<T>() where T : DbContext {
        return typeof (T).GetProperties().Where(p => p.PropertyType.GetTypeInfo()
                                                      .Name.StartsWith("DbSet"))
                         .Select(propertyInfo => propertyInfo.PropertyType.GetGenericArguments()[0]).ToList();
    }


   private static List<string> GetDbSetTypes(IEnumerable<PropertyInfo> propList) {
        var modelTypeNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
                                     .Select(p => p.PropertyType.GenericTypeArguments[0].Name)
                                     .ToList();
        return modelTypeNames;
    }

    private static List<string> GetDbSetNames(IEnumerable<PropertyInfo> propList) {
        var modelNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
                                 .Select(p => p.Name)
                                 .ToList();

        return modelNames;
    }
//DBSet Types是用于DBSet的泛型类型POCO名称
公共静态列表GetModelTypes(此DbContext上下文){
var propList=context.GetType().GetProperties();
返回getdbsetypes(propList);
}
//DBSet类型POCO类型作为IEnumerable列表
公共静态IEnumerable GetDbSetPropertyList(),其中T:DbContext{
返回typeof(T).GetProperties().Where(p=>p.PropertyType.GetTypeInfo())
.Name.StartsWith(“DbSet”))
.Select(propertyInfo=>propertyInfo.PropertyType.GetGenericArguments()[0]).ToList();
}
私有静态列表GetDbSetTypes(IEnumerable propList){
var modelTypeNames=propList.Where(p=>p.PropertyType.GetTypeInfo().Name.StartsWith(“DbSet”))
.Select(p=>p.PropertyType.GenericTypeArguments[0].Name)
.ToList();
返回modelTypeNames;
}
私有静态列表GetDbSetNames(IEnumerable propList){
var modelNames=propList.Where(p=>p.PropertyType.GetTypeInfo().Name.StartsWith(“DbSet”))
.Select(p=>p.Name)
.ToList();
返回模型名;
}
但是,您仍然需要使用employee dynamic lambda才能完成。 在这里继续这个话题:

编辑: 添加指向解决公共基本配置类方法的另一个问题的链接
如果不使用自定义约定,则开始动态方法

 modelBuilder.Entity<Location>().Property(s => s.Id).HasColumnName("LocationId");
以下是我用于此类解决方案的扩展

    // DBSet Types is the Generic Types POCO name  used for a DBSet
    public static List<string> GetModelTypes(this DbContext context) {
        var propList = context.GetType().GetProperties();
        return GetDbSetTypes(propList);
    }

    // DBSet Types POCO types as IEnumerable List
    public static IEnumerable<Type> GetDbSetPropertyList<T>() where T : DbContext {
        return typeof (T).GetProperties().Where(p => p.PropertyType.GetTypeInfo()
                                                      .Name.StartsWith("DbSet"))
                         .Select(propertyInfo => propertyInfo.PropertyType.GetGenericArguments()[0]).ToList();
    }


   private static List<string> GetDbSetTypes(IEnumerable<PropertyInfo> propList) {
        var modelTypeNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
                                     .Select(p => p.PropertyType.GenericTypeArguments[0].Name)
                                     .ToList();
        return modelTypeNames;
    }

    private static List<string> GetDbSetNames(IEnumerable<PropertyInfo> propList) {
        var modelNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
                                 .Select(p => p.Name)
                                 .ToList();

        return modelNames;
    }
//DBSet Types是用于DBSet的泛型类型POCO名称
公共静态列表GetModelTypes(此DbContext上下文){
var propList=context.GetType().GetProperties();
返回getdbsetypes(propList);
}
//DBSet类型POCO类型作为IEnumerable列表
公共静态IEnumerable GetDbSetPropertyList(),其中T:DbContext{
返回typeof(T).GetProperties().Where(p=>p.PropertyType.GetTypeInfo())
.Name.StartsWith(“DbSet”))
.Select(propertyInfo=>propertyInfo.PropertyType.GetGenericArguments()[0]).ToList();
}
私有静态列表GetDbSetTypes(IEnumerable propList){
var modelTypeNames=propList.Where(p=>p.PropertyType.GetTypeInfo().Name.StartsWith(“DbSet”))
.Select(p=>p.PropertyType.GenericTypeArguments[0].Name)
.ToList();
返回modelTypeNames;
}
私有静态列表GetDbSetNames(IEnumerable propList){
var modelNames=propList.Where(p=>p.PropertyType.GetTypeInfo().Name.StartsWith(“DbSet”))
.Select(p=>p.Name)
.ToList();
返回模型名;
}
但是,您仍然需要使用employee dynamic lambda才能完成。 在这里继续这个话题:

编辑: 添加指向解决公共基本配置类方法的另一个问题的链接

你应该看看。您需要EF6才能正常工作,但看起来您已经在使用它了。
为了给您一个概述,请看一下我用于将PascalCase名称转换为下划线名称的以下约定。它包括id属性的约定。。。它还包括一个可选的表名前缀

公共类下划线命名约定:IConfigurationConvention,
IConfigurationConvention
{
公共协议()
{
IdFieldName=“Id”;
}
公共字符串TableNamePrefix{get;set;}
公共字符串IdFieldName{get;set;}
公共无效应用(PropertyInfo PropertyInfo,Func配置)
{
var columnName=propertyInfo.Name;
如果(propertyInfo.Name==IdFieldName)
columnName=propertyInfo.ReflectedType.Name+IdFieldName;
configuration().ColumnName=ToUnderscore(ColumnName);
}
公共无效应用(类型、函数配置)
{
var entityTypeConfiguration=configuration().实体(类型);
if(entityTypeConfiguration.istableNameconfigurated)返回;
var tableName=ToUnderscore(type.Name);
如果(!string.IsNullOrEmpty(TableNamePrefix))
{
tableName=string.Format(“{0}{1}”,tableName前缀,tableName);
}
entityTypeConfiguration.ToTable(表名);
}
公共静态字符串ToUnderscore(字符串值)
{
public void Apply(PropertyInfo propertyInfo, Func<PrimitivePropertyConfiguration> configuration)
{
    if (propertyInfo.Name == "Id")
    {
        configuration().ColumnName = propertyInfo.ReflectedType.Name + "Id";
    }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Properties<int>()
      .Where(p => p.Name.Equals("Id"))
      .Configure(c => c.HasColumnName(c.ClrPropertyInfo.ReflectedType.Name + "Id"));
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        try
        {
            _builder = modelBuilder;
            var typeName = typeof(T).Name;

            _builder
                .Entity(typeof(T))
                .Property<int>("Id")
                .ForSqliteHasColumnName(typeName + "Id");
        }

        catch (Exception e)
        {
            throw e;
        }
}
modelBuilder.Entity<roles>().Property(b => b.id).HasColumnName("role_id");
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long id { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long role_id { get; set; }
//modelBuilder.Entity<roles>().Property(b => b.id).HasColumnName("role_id");