[英]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 boolean
为nullable 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.