简体   繁体   中英

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>>

Essentially its this 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.

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() . A problem arises if there's a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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