/// <summary> /// Custom Many. /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="KTargetEntity"></typeparam> /// <param name="mapper"></param> /// <param name="propertyName"></param> /// <returns></returns> public static DependentNavigationPropertyConfiguration <TEntity> WithMany <TEntity, KTargetEntity>( this OptionalNavigationPropertyConfiguration <TEntity, KTargetEntity> mapper, String propertyName) where TEntity : class where KTargetEntity : class { Type type = typeof(KTargetEntity); ParameterExpression argumentExpression = Expression.Parameter(type, "x"); PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); MemberExpression memberExpression = Expression.Property(argumentExpression, pi); LambdaExpression lambda = Expression.Lambda(memberExpression, argumentExpression); var expression = (Expression <Func <KTargetEntity, ICollection <TEntity> > >)lambda; return(mapper.WithMany(expression)); }
/// <summary> /// Configures an optional relationship from this entity type. /// Instances of the entity type will be able to be saved to the database without this relationship being specified. /// The foreign key in the database will be nullable. /// </summary> /// <typeparam name="TTargetEntity"> The type of the entity at the other end of the relationship. </typeparam> /// <param name="navigationPropertyExpression"> A lambda expression representing the navigation property /// for the relationship. C#: t => t.MyProperty VB.Net: Function(t) t.MyProperty </param> /// <returns> A configuration object that can be used to further configure the relationship. </returns> public OptionalNavigationPropertyConfiguration <TEntityType, TTargetEntity> HasOptional <TTargetEntity>( Expression <Func <TEntityType, TTargetEntity> > navigationPropertyExpression) where TTargetEntity : class { if (navigationPropertyExpression == null) { throw new ArgumentNullException("navigationPropertyExpression"); } var navigationProperty = ReflectionExpressions.GetPropertyInfo(navigationPropertyExpression); IOptionalNavigationPropertyConfiguration propertyConfiguration; if (!optionalAssociationsByProperty.TryGetValue(navigationProperty, out propertyConfiguration)) { propertyConfiguration = new OptionalNavigationPropertyConfiguration <TEntityType, TTargetEntity>(navigationProperty); optionalAssociationsByProperty.Add(navigationProperty, propertyConfiguration); } return((OptionalNavigationPropertyConfiguration <TEntityType, TTargetEntity>)propertyConfiguration); }