Beispiel #1
0
        protected List <MappedProperty> GetPropertiesHierarchy(Type propertyType, List <string> parentPropertyNames)
        {
            if (parentPropertyNames.Count == 2)
            {
                throw new NotImplementedException("More than 1 level of Entity framework owned entities is not supported.");
            }

            List <MappedProperty> list = new List <MappedProperty>();

            //Include all primitive properties
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

            PropertyInfo[]      allEntityProperties = propertyType.GetProperties(bindingFlags);
            List <PropertyInfo> primitiveProperties = allEntityProperties.Where(
                p =>
            {
                Type underlying = Nullable.GetUnderlyingType(p.PropertyType);
                Type property   = underlying ?? p.PropertyType;
                return(property.IsValueType == true ||
                       property.IsPrimitive ||
                       property == typeof(string));
            })
                                                      .ToList();

            //Exclude ignored properties
            IEntityType efEntityType = null;

            if (propertyType == _rootEntityType)
            {
                efEntityType = _efRootEntityType;
            }
            else
            {
                string propertyName = parentPropertyNames[0];
                efEntityType = _context.GetOwnedProperty(_efRootEntityType, propertyName);
            }

            List <string> efMappedProperties = efEntityType.GetProperties()
                                               .Select(x => x.Name)
                                               .ToList();

            primitiveProperties = primitiveProperties
                                  .Where(x => efMappedProperties.Contains(x.Name))
                                  .ToList();

            //Return all the rest properties
            foreach (PropertyInfo supportedProp in primitiveProperties)
            {
                List <string> namesHierarchy = parentPropertyNames.ToList();
                namesHierarchy.Add(supportedProp.Name);

                string    efDefaultName = ReflectionUtility.ConcatenateEfPropertyName(namesHierarchy);
                IProperty property      = DbContextExtensions.GetPropertyMapping(_context, _rootEntityType, efDefaultName);
                list.Add(new MappedProperty
                {
                    PropertyInfo      = supportedProp,
                    EfDefaultName     = efDefaultName,
                    EfMappedName      = property.GetColumnName(),
                    ConfiguredSqlType = property.GetColumnType()
                });
            }

            //Add complex properties
            List <MappedProperty> complexProperties = GetComplexProperties(allEntityProperties, parentPropertyNames);

            list.AddRange(complexProperties);
            return(list);
        }