简体   繁体   English

首先使用实体​​框架代码,在布尔属性上设置列名称

[英]Entity Framework code first, set column name on a Boolean property

Im attempting to set the column name on a boolean property using reflection. 我试图使用反射在布尔属性上设置列名称。 This works for the standard String, Int etc but there is no Property method that takes a Expression<Func<Object,Boolean>> 这适用于标准String,Int等,但是没有采用Expression<Func<Object,Boolean>> Property方法

Essentially its this modelBuilder.Entity<Object>().Property(g => g.boolDeleted).HasColumnName("BooleanColumn"); 本质上讲,它是这个modelBuilder.Entity<Object>().Property(g => g.boolDeleted).HasColumnName("BooleanColumn");

ParameterExpression parameter = Expression.Parameter(entityObject, "t");
var expressionProperty = Expression.Property(parameter, propertyInfo.Name);

const string methodSignature = "System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate]" + "(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])";
var generatedLambdaMethod = typeof(Expression).GetMethods()
.Single(mi => mi.ToString() == methodSignature);

var func = typeof(Func<,>).MakeGenericType(new[] { entityObject, typeof(Boolean) });

var genericLambda = generatedLambdaMethod.MakeGenericMethod(func);

var generatedLambdaInvoked = genericLambda.Invoke(null,
new object[] { expressionProperty, new[] { parameter } });

var method = modelBuilder.GetType().GetMethod("Entity");

var genericMethod = method.MakeGenericMethod(new[] { entityObject })
    .Invoke(modelBuilder, new object[] { });

var propertyMethod = genericMethod.GetType()
    .GetMethods()
    .Where(m => m.Name == "Property" && m.GetParameters().Where(p => p.ParameterType == generatedLambdaInvoked .GetType()).Any() == true)
    .Single();

var propInvoked = propertyMethod.Invoke(genericMethod, new[] { generatedLambdaInvoked });                       


var hasColumnNameInvoked = propInvoked.GetType()
    .GetMethods()
    .Where(m => m.Name == "HasColumnName")
    .First()
    .Invoke(propInvoked, new object[] { "BooleanColumn" });

What do I do to set the name of this column? 如何设置此列的名称? EF can read the column so this cant be out of the question. EF可以阅读该专栏,因此这不可能。

var propertyMethods = genericMethod.GetType()
    .GetMethods()
    .Where(m => m.Name == "Property" && m.IsGenericMethodDefinition)
    .First()
    .MakeGenericMethod(new[] { typeof(Boolean) });

I hope this helps someone in the future, Theres an optional generic constructor for the method Property() . 我希望这对以后的人有所帮助,为Property()方法提供了一个可选的泛型构造函数。 A problem arises if there's a nullable boolean 如果存在nullable booleannullable boolean ,则会出现问题

this is what I did. 这就是我所做的。

var propertyMethods = genericMethod.GetType()
    .GetMethods()
    .Where(m => m.Name == "Property" && m.IsGenericMethodDefinition)
    .ToList();

if (Nullable.GetUnderlyingType(typeof(Boolean?)) != null)
{
    propertyMethod = propertyMethods.ElementAt(1)
        .MakeGenericMethod(new[] { Nullable.GetUnderlyingType(typeof(Boolean?)) });

}
else
{
    propertyMethod = propertyMethods.ElementAt(0)
        .MakeGenericMethod(new[] { typeof(Boolean) });
}

So really you call the generic method that takes a Nullable, but you make the generic with the non-nullable type. 因此,实际上您调用了采用Nullable的泛型方法,但是您将泛型与非Nullable类型一起使用。

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

相关问题 实体框架代码第一列名称问题 - Entity Framework Code First column name issue 实体框架代码优先 - 虚拟财产列命名 - Entity Framework Code First - Virtual Property Column Naming 实体框架代码第一个布尔外键 - Entity Framework Code First boolean foreign key 在实体框架代码优先迁移中选择列名 - Choosing name of column in entity framework code-first migrations 实体框架-首先从数据库“无效的列名&#39;discriminator&#39;&#39;开始编码 - Entity Framework - Code first from database “invalid column name 'discriminator'” 实体框架 - 大写第一个属性名称字母 - Entity Framework - Capitalizing first property name letter 首先从实体框架6数据库中的数据库中获取列名,并在映射中使用不同的属性名称 - Get the column name from the database in entity framework 6 database first with different property name in mapping 实体框架代码首先添加 DbSet 属性 - Entity Framework code first adding DbSet property 首先使用实体​​框架代码-保存实体时设置属性的最佳方法是什么 - Entity framework code first - What is the best approch to set property when entity is saved 实体框架列名称到属性映射数据库 - entity framework column name to property mapping dbentry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM