Beispiel #1
0
 public static ManyToManyNavigationPropertyConfiguration WithMany <TEntityType, TTargetEntityType>(this ManyNavigationPropertyConfiguration <TEntityType, TTargetEntityType> many, string propertyName)
     where TEntityType : class
     where TTargetEntityType : class
 {
     // NB: this cast will blow up at runtime (i.e. during entity mapper setup) if the nominated property is not of type ICollection<TTargetEntityType>
     return(many.WithMany((Expression <Func <TTargetEntityType, ICollection <TEntityType> > >) typeof(TTargetEntityType).GetPropertyAccessExpression(propertyName)));
 }
 public ManyNavigationPropertyConfigurationWrapper(ManyNavigationPropertyConfiguration <TEntity, TTargetEntity> manyNavigationPropertyConfiguration)
 {
     this.manyNavigationPropertyConfiguration = manyNavigationPropertyConfiguration;
 }
        /// <summary>
        /// Extension method for map inverse of many navigation property
        /// </summary>
        /// <typeparam name="TEntityType">The parent of parent in relation and the inverse in this method</typeparam>
        /// <typeparam name="TTargetEntityType">The target in many:required</typeparam>
        /// <param name="manyNavigationPropertyConfiguration">The navigation property configuration</param>
        /// <param name="navigationPropertyName">The property name</param>
        /// <returns>A DependentNavigationPropertyConfiguration for this many:required navigation </returns>
        public static DependentNavigationPropertyConfiguration <TTargetEntityType> WithRequired <TEntityType, TTargetEntityType>(this ManyNavigationPropertyConfiguration <TEntityType, TTargetEntityType> manyNavigationPropertyConfiguration, string navigationPropertyName)
            where TEntityType : class
            where TTargetEntityType : class
        {
            var propertyInfo = typeof(TTargetEntityType).GetProperty(navigationPropertyName,
                                                                     BindingFlags.NonPublic |
                                                                     BindingFlags.Instance |
                                                                     BindingFlags.Public);

            if (propertyInfo != null) // if private property exists
            {
                ParameterExpression arg = Expression.Parameter(typeof(TTargetEntityType), "parameterName");
                MemberExpression    memberExpression = Expression.Property((Expression)arg, propertyInfo);

                //Create the expression to map
                Expression <Func <TTargetEntityType, TEntityType> > expression = (Expression <Func <TTargetEntityType, TEntityType> >)Expression.Lambda(memberExpression, arg);


                return(manyNavigationPropertyConfiguration.WithRequired(expression));
            }
            else
            {
                throw new InvalidOperationException("The property not exist");
            }
        }