Esempio n. 1
0
        /// <summary>
        /// 需要同时加载外键
        /// </summary>
        /// <typeparam name="TFKEntity"></typeparam>
        /// <param name="fkEntityExp">
        /// 需要加载的外键实体属性表达式
        /// </param>
        /// <returns></returns>
        public PropertySelector <TFKEntity> LoadFK <TFKEntity>(Expression <Func <TEntity, TFKEntity> > fkEntityExp)
            where TFKEntity : Entity
        {
            var entityPropertyName = GetPropertyName(fkEntityExp);
            var propertyName       = entityPropertyName + Entity.IdProperty.Name;

            var entityInfo   = CommonModel.Entities.Get(typeof(TEntity));
            var propertyInfo = entityInfo.EntityProperties.FirstOrDefault(p => p.Name == propertyName);

            //构造一个临时代理方法,实现:TEntity.EntityProperty = TFKEntity
            var pE          = Expression.Parameter(typeof(TEntity), "e");
            var pEFK        = Expression.Parameter(typeof(TFKEntity), "efk");
            var propertyExp = Expression.Property(pE, entityPropertyName);
            var body        = Expression.Assign(propertyExp, pEFK);
            var result      = Expression.Lambda <Action <TEntity, TFKEntity> >(body, pE, pEFK);
            var fkSetter    = result.Compile();

            var option = new LoadOptionItem(propertyInfo, (e, eFK) => fkSetter(e as TEntity, eFK as TFKEntity));

            //避免循环
            if (this.InnerDescriptor.Items.Any(i => i.OwnerType == option.PropertyEntityType))
            {
                throw new InvalidOperationException("有循环的实体设置。");
            }

            this.InnerDescriptor.AddItem(option);

            return(new PropertySelector <TFKEntity>(this.InnerDescriptor));
        }
Esempio n. 2
0
 /// <summary>
 /// 追加一个聚合加载选项
 /// </summary>
 /// <param name="item"></param>
 internal void AddItem(LoadOptionItem item)
 {
     this._items.AddLast(item);
 }