Created
November 22, 2012 22:07
-
-
Save anonymous/4133105 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ContextUtils | |
{ | |
public static void FixDateTimeColumns(DbContext context, DbModelBuilder modelBuilder) | |
{ | |
var contextProps = context.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); | |
foreach (var contextProp in contextProps) { | |
var propType = contextProp.PropertyType; | |
if (!propType.IsGenericType) | |
continue; | |
if (propType.GetGenericTypeDefinition()!=typeof(DbSet<>)) | |
continue; | |
var modelType = propType.GetGenericArguments()[0]; | |
// call generic Entity<> method | |
var entityTypeConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(modelType).Invoke(modelBuilder, null); | |
var modelProps = modelType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); | |
foreach (var modelProp in modelProps) { | |
var modelPropType = modelProp.PropertyType; | |
if (modelPropType != typeof(DateTime)) | |
continue; | |
// prepare lambda (Expression<Func<TStructuralType, DateTime>>) | |
var typeParam = Expression.Parameter(modelType, "p"); | |
var body = Expression.Property(typeParam, modelProp); | |
var lambda = Expression.Lambda(body, typeParam); | |
// call Property method | |
var propertyMethod = GetPropertyMethod(entityTypeConfig); | |
var propConfig = (DateTimePropertyConfiguration)propertyMethod.Invoke(entityTypeConfig, new[] { lambda }); | |
// set the type | |
propConfig.HasColumnType("datetime2"); | |
} | |
} | |
} | |
private static MethodInfo GetPropertyMethod(object entityTypeConfig) | |
{ | |
foreach (var method in entityTypeConfig.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public)) { | |
if (method.Name == "Property") { | |
var mParams = method.GetParameters(); | |
if (mParams.Length == 1) { | |
var p = mParams[0]; | |
var f = p.ParameterType.GetGenericArguments()[0]; // Func<TStructuralType, DateTime> | |
var args = f.GetGenericArguments(); | |
if (args.Length == 2 && args[1] == typeof(DateTime)) { | |
return method; | |
} | |
} | |
} | |
} | |
throw new InvalidOperationException("Could not find appropriate Property method."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment