コード例 #1
0
        public IPrimitivePropertyBuilder Property(string property)
        {
            PrimitiveProperty entityProperty = this.EntityType.PrimitiveProperties.FirstOrDefault(a => a.Property.Name == property);

            if (entityProperty == null)
            {
                throw new ArgumentException($"The mapping property list doesn't contain property named '{property}'.");
            }

            IPrimitivePropertyBuilder propertyBuilder = Activator.CreateInstance(typeof(PrimitivePropertyBuilder <>).MakeGenericType(entityProperty.Property.PropertyType), entityProperty) as IPrimitivePropertyBuilder;

            return(propertyBuilder);
        }
コード例 #2
0
ファイル: EntityType.cs プロジェクト: chengxulvtu/Chloe
        public EntityType(Type type)
        {
            this.Type      = type;
            this.TableName = type.Name;

            PropertyInfo[] properties = this.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(a => a.GetSetMethod() != null && a.GetGetMethod() != null).ToArray();

            foreach (PropertyInfo property in properties)
            {
                MappingType mappingType;
                if (!MappingTypeSystem.IsMappingType(property.PropertyType, out mappingType))
                {
                    continue;
                }

                PrimitiveProperty primitiveProperty = new PrimitiveProperty(property);
                primitiveProperty.DbType     = mappingType.DbType;
                primitiveProperty.IsNullable = property.PropertyType.CanNull();

                if (string.Equals(property.Name, "id", StringComparison.OrdinalIgnoreCase))
                {
                    /* 默认为主键 */
                    primitiveProperty.IsPrimaryKey = true;

                    if (Utils.IsAutoIncrementType(property.PropertyType))
                    {
                        primitiveProperty.IsAutoIncrement = true;
                    }

                    if (property.PropertyType == typeof(string))
                    {
                        //如果主键是 string 类型,默认为 AnsiString
                        primitiveProperty.DbType = System.Data.DbType.AnsiString;
                    }
                }

                this.PrimitiveProperties.Add(primitiveProperty);
            }
        }
コード例 #3
0
        void ConfigureColumnMapping()
        {
            var propertyInfos = this.EntityType.PrimitiveProperties.Select(a => a.Property).ToList();

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                IPrimitivePropertyBuilder propertyBuilder = this.Property(propertyInfo.Name);

                propertyBuilder.IsPrimaryKey(false);
                propertyBuilder.IsAutoIncrement(false);

                var propertyAttributes = propertyInfo.GetCustomAttributes();
                foreach (Attribute propertyAttribute in propertyAttributes)
                {
                    propertyBuilder.HasAnnotation(propertyAttribute);

                    if (propertyAttribute is ColumnAttribute)
                    {
                        ColumnAttribute columnAttribute = (ColumnAttribute)propertyAttribute;

                        if (!string.IsNullOrEmpty(columnAttribute.Name))
                        {
                            propertyBuilder.MapTo(columnAttribute.Name);
                        }

                        if (columnAttribute.HasDbType())
                        {
                            propertyBuilder.HasDbType(columnAttribute.DbType);
                        }

                        propertyBuilder.IsPrimaryKey(columnAttribute.IsPrimaryKey);
                        propertyBuilder.IsRowVersion(columnAttribute.IsRowVersion);
                        propertyBuilder.HasSize(columnAttribute.GetSize());
                        propertyBuilder.HasScale(columnAttribute.GetScale());
                        propertyBuilder.HasPrecision(columnAttribute.GetPrecision());

                        continue;
                    }

                    if (propertyAttribute is NotMappedAttribute)
                    {
                        this.Ignore(propertyInfo.Name);
                        continue;
                    }

                    if (propertyAttribute is NotNullAttribute)
                    {
                        propertyBuilder.IsNullable(false);
                        continue;
                    }

                    if (propertyAttribute is AutoIncrementAttribute)
                    {
                        propertyBuilder.IsAutoIncrement(true);
                        continue;
                    }

                    if (propertyAttribute is UpdateIgnoreAttribute)
                    {
                        propertyBuilder.UpdateIgnore(true);
                        continue;
                    }

                    SequenceAttribute sequenceAttribute = propertyAttribute as SequenceAttribute;
                    if (sequenceAttribute != null)
                    {
                        propertyBuilder.HasSequence(sequenceAttribute.Name, sequenceAttribute.Schema);
                    }
                }
            }

            List <PrimitiveProperty> primaryKeys = this.EntityType.PrimitiveProperties.Where(a => a.IsPrimaryKey).ToList();

            if (primaryKeys.Count == 0)
            {
                //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键
                PrimitiveProperty idNameProperty = this.EntityType.PrimitiveProperties.Find(a => string.Equals(a.Property.Name, "id", StringComparison.OrdinalIgnoreCase) && !a.Property.IsDefined(typeof(ColumnAttribute)));

                if (idNameProperty != null)
                {
                    this.Property(idNameProperty.Property.Name).IsPrimaryKey();
                    primaryKeys.Add(idNameProperty);
                }
            }

            if (primaryKeys.Count == 1 && this.EntityType.PrimitiveProperties.Count(a => a.IsAutoIncrement) == 0)
            {
                /* 如果没有显示定义自增成员,并且主键只有 1 个,如果该主键满足一定条件,则默认其是自增列 */
                PrimitiveProperty primaryKey = primaryKeys[0];

                if (string.IsNullOrEmpty(primaryKey.SequenceName) && Utils.IsAutoIncrementType(primaryKey.Property.PropertyType) && !primaryKey.Property.IsDefined(typeof(NonAutoIncrementAttribute)))
                {
                    this.Property(primaryKey.Property.Name).IsAutoIncrement();
                }
            }
        }
コード例 #4
0
 public PrimitivePropertyBuilder(PrimitiveProperty property)
 {
     this.Property = property;
 }