Exemple #1
0
        internal PropertyBuilder(AbstractEntity entity, MemberInfo memberInfo, ConfigurationSource configurationSource)
        {
            var propertyName = memberInfo.GetSimpleMemberName();

            _property = entity.GetProperty(propertyName);

            if (_property == default)
            {
                _property = new Property(
                    propertyName,
                    memberInfo.GetMemberType(),
                    memberInfo as PropertyInfo,
                    configurationSource
                    );
                entity.AddProperty(_property);
            }
        }
Exemple #2
0
        private static void AnalyzeType(AbstractEntity entity)
        {
            foreach (var propertyInfo in entity.Type.GetProperties())
            {
                if (!propertyInfo.CanWrite || !propertyInfo.CanRead)
                {
                    continue;
                }

                var property = entity.GetProperty(propertyInfo.Name);
                if (property != default && property.IsIgnored)
                {
                    continue;
                }
                else if (property == default && ShouldIgnoreProperty(propertyInfo))
                {
                    property = new Property(propertyInfo.Name, propertyInfo.PropertyType, propertyInfo, Enums.ConfigurationSource.DataAnnotation);
                    property.Ignore();
                    entity.AddProperty(property);

                    continue;
                }

                if (property == default && ShouldBeReadOnlyProperty(propertyInfo))
                {
                    property = new Property(propertyInfo.Name, propertyInfo.PropertyType, propertyInfo, Enums.ConfigurationSource.DataAnnotation);
                    property.SetIsReadOnly();
                    entity.AddProperty(property);
                }

                var propertyType = propertyInfo.PropertyType.GetDataType();

                if (propertyType.IsNoClass())
                {
                    property = entity.GetProperty(propertyInfo.Name);
                    if (property == default)
                    {
                        property = new Property(propertyInfo.Name, propertyInfo.PropertyType, propertyInfo, Enums.ConfigurationSource.Convention);
                        entity.AddProperty(property);
                    }

                    if (!property.IsPrimaryKey && propertyInfo.IsPrímaryKey())
                    {
                        property.SetPrimiaryKey(true, Enums.ConfigurationSource.DataAnnotation);
                    }

                    if (property.AutoGeneratedOption == DatabaseGeneratedOption.None)
                    {
                        if (property.IsPrimaryKey)
                        {
                            property.SetAutoGeneratedOption(IsAutoGenerated(propertyInfo));
                        }
                        else
                        {
                            property.SetAutoGeneratedOption(propertyInfo.GetCustomAttribute <DatabaseGeneratedAttribute>()?.DatabaseGeneratedOption ?? DatabaseGeneratedOption.None);
                        }
                    }
                }
                else if (propertyType.IsClass() || Storm.Models.TypeHandlerMap.Map.ContainsKey(propertyType))
                {
                    property = entity.GetProperty(propertyInfo.Name);
                    if (property == default || !property.IsOwnsOne)
                    {
                        if (propertyInfo.IsOwnsOne())
                        {
                            if (property == default)
                            {
                                property = new Property(propertyInfo.Name, propertyInfo.PropertyType, propertyInfo, Enums.ConfigurationSource.DataAnnotation);
                                entity.AddProperty(property);
                            }

                            var ownsOneEntity = new OwnsOneEntity(propertyInfo.PropertyType, Enums.ConfigurationSource.DataAnnotation);
                            property.SetOwnsOneEntity(ownsOneEntity);
                            AnalyzeType(ownsOneEntity);
                        }
                    }
                    else if (property.IsOwnsOne && !property.OwnsOne.GetProperties().Any())
                    {
                        AnalyzeType(property.OwnsOne);
                    }

                    if (property == default || !property.IsOwnsOne)
                    {
                        propertyInfo.PropertyType.LookupDbType("", false, out var _);
                        if (Storm.Models.TypeHandlerMap.Map.ContainsKey(propertyInfo.PropertyType))
                        {
                            property = entity.GetProperty(propertyInfo.Name);
                            if (property == default)
                            {
                                property = new Property(propertyInfo.Name, propertyInfo.PropertyType, propertyInfo, Enums.ConfigurationSource.DataAnnotation);
                                entity.AddProperty(property);
                            }
                        }
                    }
                }

                if (property != default && property.ColumnName.IsNullOrEmpty())
                {
                    property.SetColumnName(propertyInfo.GetColumnName());
                }
            }
        }