예제 #1
0
        public IEntityPropertyBuilder <TProperty> Property <TProperty>(Expression <Func <TEntity, TProperty> > property)
        {
            string propertyName = PropertyNameExtractor.Extract(property);
            IEntityPropertyBuilder <TProperty> propertyBuilder = this.Property(propertyName) as IEntityPropertyBuilder <TProperty>;

            return(propertyBuilder);
        }
예제 #2
0
        public IEntityPropertyBuilder Property(string property)
        {
            EntityProperty entityProperty = this.EntityType.Properties.FirstOrDefault(a => a.Property.Name == property);

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

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

            return(propertyBuilder);
        }
예제 #3
0
 public EntityBuilder(IModelDataStorage <Type> storage)
 {
     storage_ = storage;
     entityRelationshipFactory_ = new EntityRelationshipFactory();
     entityPropertyBuilder_     = new EntityPropertyBuilder <TEntity>(storage_);
 }
예제 #4
0
        void ConfigureColumnMapping()
        {
            var propertyInfos = this.EntityType.Properties.Select(a => a.Property).ToList();

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

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

                var propertyAttributes = propertyInfo.GetCustomAttributes();
                foreach (Attribute propertyAttribute in propertyAttributes)
                {
                    if (propertyAttribute is NotMappedAttribute)
                    {
                        this.Ignore(propertyInfo.Name);
                    }

                    propertyBuilder.HasAnnotation(propertyAttribute);

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

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

                        propertyBuilder.IsPrimaryKey(columnAttribute.IsPrimaryKey);
                        propertyBuilder.HasDbType(columnAttribute.GetDbType());
                        propertyBuilder.HasSize(columnAttribute.GetSize());
                        propertyBuilder.HasScale(columnAttribute.GetScale());
                        propertyBuilder.HasPrecision(columnAttribute.GetPrecision());
                    }

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

                    if (propertyAttribute is SequenceAttribute)
                    {
                        propertyBuilder.HasSequence((propertyAttribute as SequenceAttribute).Name);
                    }
                }
            }

            List <EntityProperty> primaryKeys = this.EntityType.Properties.Where(a => a.IsPrimaryKey).ToList();

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

                if (idNameProperty != null)
                {
                    idNameProperty.IsPrimaryKey = true;
                    primaryKeys.Add(idNameProperty);
                }
            }

            if (primaryKeys.Count == 1 && this.EntityType.Properties.Count(a => a.IsAutoIncrement) == 0)
            {
                /* 如果没有显示定义自增成员,并且主键只有 1 个,如果该主键满足一定条件,则默认其是自增列 */
                EntityProperty primaryKey = primaryKeys[0];
                if (Utils.IsAutoIncrementType(primaryKey.Property.PropertyType) && !primaryKey.Property.IsDefined(typeof(NonAutoIncrementAttribute)))
                {
                    primaryKey.IsAutoIncrement = true;
                }
            }
        }
예제 #5
0
 /// <summary>
 /// 标识字段为 timestamp 类型
 /// </summary>
 /// <param name="entityPropertyBuilder"></param>
 /// <returns></returns>
 public static IEntityPropertyBuilder IsTimestamp(this IEntityPropertyBuilder entityPropertyBuilder)
 {
     return(entityPropertyBuilder.HasAnnotation(new TimestampAttribute()));
 }