-
-
Save mahizsas/9e488cb2bf0460898886 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 DataContextExtensions | |
{ | |
/// <summary> | |
/// Map private collection property | |
/// </summary> | |
/// <typeparam name="T">Entity type</typeparam> | |
/// <typeparam name="R">Collection entity</typeparam> | |
/// <param name="propertyName">property name</param> | |
public static ManyNavigationPropertyConfiguration<T, R> HasMany<T, R>(this EntityTypeConfiguration<T> mapper, string propertyName) | |
where T : class | |
where R : class | |
{ | |
Type type = typeof(T); | |
ParameterExpression arg = Expression.Parameter(type, "x"); | |
Expression expr = arg; | |
PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); | |
expr = Expression.Property(expr, pi); | |
LambdaExpression lambda = Expression.Lambda(expr, arg); | |
var expression = (Expression<Func<T, ICollection<R>>>)lambda; | |
return mapper.HasMany(expression); | |
} | |
/// <summary> | |
/// Map private property | |
/// </summary> | |
/// <typeparam name="T">Entity type</typeparam> | |
/// <param name="propertyName">property name</param> | |
public static StringPropertyConfiguration Property<T>(this EntityTypeConfiguration<T> mapper, string propertyName) where T : class | |
{ | |
Type type = typeof(T); | |
ParameterExpression arg = Expression.Parameter(type, "x"); | |
Expression expr = arg; | |
PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); | |
expr = Expression.Property(expr, pi); | |
LambdaExpression lambda = Expression.Lambda(expr, arg); | |
var expression = (Expression<Func<T, string>>)lambda; | |
return mapper.Property(expression); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment