Пример #1
0
        public object Property <TModel>(DbModelBuilder modelBuilder) where TModel : class
        {
            EntityTypeConfiguration <TModel> entityTypeConfiguration = modelBuilder.Entity <TModel>();

            List <MethodInfo> propertyMethods = entityTypeConfiguration.GetType().GetMethods().Where(m => m.Name == nameof(StructuralTypeConfiguration <TModel> .Property)).ToList();
            MethodInfo        propertyMethod  = null;

            //Grab any methods that accept this property type OR are generic
            propertyMethods = propertyMethods.Where(p =>
            {
                Type t = p.GetParameters()[0].ParameterType.GetGenericArguments()[0].GetGenericArguments()[1];
                return(t.IsGenericParameter || t.IsAssignableFrom(this.Member.PropertyType));
            }).ToList();

            //Assuming we get a generic and a nongeneric, we choose the nongeneric
            if (propertyMethods.Count > 1)
            {
                propertyMethods = propertyMethods.Where(p =>
                {
                    Type t = p.GetParameters()[0].ParameterType.GetGenericArguments()[0].GetGenericArguments()[1];
                    return(t.IsAssignableFrom(this.Member.PropertyType));
                }).ToList();

                propertyMethod = propertyMethods.Single();
            }
            else
            {
                //Otherwise we assume we only have the generic so we use that
                propertyMethod = propertyMethods.Single().MakeGenericMethod(this.Member.PropertyType);
            }

            return(propertyMethod.Invoke(entityTypeConfiguration, new[] { PropertyExpression(this.Member.ReflectedType, this.Member.Name) }));
        }
Пример #2
0
        public object PropertyMethod <TModel>(DbModelBuilder modelBuilder, string Name) where TModel : class
        {
            EntityTypeConfiguration <TModel> entityTypeConfiguration = modelBuilder.Entity <TModel>();

            List <MethodInfo> propertyMethods = entityTypeConfiguration.GetType().GetMethods().Where(m => m.GetParameters().Length == 1 && m.Name == Name && m.ContainsGenericParameters).ToList();

            MethodInfo propertyMethod = propertyMethods.Single().MakeGenericMethod(this.Member.PropertyType);

            return(propertyMethod.Invoke(entityTypeConfiguration, new[] { PropertyExpression(this.Member.ReflectedType, this.Member.Name) }));
        }
Пример #3
0
        public static object MapMany <TModel>(System.Data.Entity.DbModelBuilder modelBuilder, PropertyInfo p, Type propertyOverrideType = null) where TModel : class
        {
            Type t = typeof(TModel);

            EntityTypeConfiguration <TModel> entityTypeConfiguration = modelBuilder.Entity <TModel>();

            Type collectionType = (propertyOverrideType ?? p.PropertyType).GetGenericArguments()[0];

            MethodInfo HasManyMethod = entityTypeConfiguration.GetType().GetMethod(nameof(EntityTypeConfiguration <object> .HasMany)).MakeGenericMethod(collectionType);

            return(HasManyMethod.Invoke(entityTypeConfiguration, new[] { PropertyExpression(t, p.Name, typeof(ICollection <>).MakeGenericType(collectionType)) }));
        }
Пример #4
0
        public override void Build <T>(DbModelBuilder modelBuilder)
        {
            Mapping mapping = this.Attribute.GetMapping(this.Member);

            EntityTypeConfiguration <T> entityTypeConfiguration = modelBuilder.Entity <T>();

            MethodInfo hasOptionalMethod = entityTypeConfiguration.GetType().GetMethod(nameof(EntityTypeConfiguration <object> .HasOptional)).MakeGenericMethod(this.Member.PropertyType);

            object optionalNavigationPropertyConfiguration = hasOptionalMethod.Invoke(entityTypeConfiguration, new[] { PropertyExpression(typeof(T), mapping.Left.Property) });

            //With Required
            MethodInfo withRequiredMethod = optionalNavigationPropertyConfiguration.GetType().GetMethods().Single(m => m.GetParameters().Length == 1 && m.Name == nameof(OptionalNavigationPropertyConfiguration <object, object> .WithRequired));

            withRequiredMethod.Invoke(optionalNavigationPropertyConfiguration, new[] { PropertyExpression(this.Member.PropertyType, mapping.Right.Property) });
        }
Пример #5
0
        public override void Build <T>(DbModelBuilder modelBuilder)
        {
            Mapping mapping = this.Attribute.GetMapping(this.Member);

            EntityTypeConfiguration <T> entityTypeConfiguration = modelBuilder.Entity <T>();

            MethodInfo hasOptionalMethod = entityTypeConfiguration.GetType().GetMethod(nameof(EntityTypeConfiguration <object> .HasOptional)).MakeGenericMethod(this.Member.PropertyType);

            object optionalNavigationPropertyConfiguration = hasOptionalMethod.Invoke(entityTypeConfiguration, new[] { PropertyExpression(typeof(T), mapping.Left.Property) });

            //With Many

            //ManyNavigationPropertyConfiguration
            WithMany(optionalNavigationPropertyConfiguration, this.Member, mapping);
        }
        public void AddConfiguration <T>(EntityTypeConfiguration <T> configuration) where T : class
        {
            if (_IsModelCreated)
            {
                return;
            }

            lock (_LockObject)
            {
                //Don't add duplicates
                if (_Configurations.Count(p => p.GetType() == configuration.GetType()) > 0)
                {
                    return;
                }
                _Configurations.Add(configuration);
            }
        }
Пример #7
0
        public override void Build <T>(DbModelBuilder modelBuilder)
        {
            Mapping mapping = this.Attribute.GetMapping(this.Member);

            //We're calling the build method on a type that doesn't match the declaring property type
            if (PropertyExpression(this.Member.PropertyType, mapping.Right.Property).ReturnType != typeof(T))
            {
                return;
            }

            EntityTypeConfiguration <T> entityTypeConfiguration = modelBuilder.Entity <T>();

            MethodInfo hasOptionalMethod = entityTypeConfiguration.GetType().GetMethod(nameof(EntityTypeConfiguration <object> .HasOptional)).MakeGenericMethod(this.Member.PropertyType);

            object optionalNavigationPropertyConfiguration = hasOptionalMethod.Invoke(entityTypeConfiguration, new[] { PropertyExpression(typeof(T), mapping.Left.Property) });

            //With Required
            MethodInfo withOptionalDependentMethod = optionalNavigationPropertyConfiguration.GetType().GetMethods().Single(m => m.GetParameters().Length == 1 && m.Name == nameof(OptionalNavigationPropertyConfiguration <object, object> .WithOptionalDependent));

            withOptionalDependentMethod.Invoke(optionalNavigationPropertyConfiguration, new[] { PropertyExpression(this.Member.PropertyType, mapping.Right.Property) });
        }
Пример #8
0
        protected void SetIdAndRowVersion <T>(EntityTypeConfiguration <T> entity) where T : class, IEntity
        {
            var tableName = entity.GetType().GenericTypeArguments[0].Name;

            SetIdAndRowVersion(entity, tableName);
        }