Ejemplo n.º 1
0
        /// <summary>
        /// 处理实体的关联的属性。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="properties"></param>
        private void HandleRelationProperties(IEntity entity, IEnumerable <IProperty> properties)
        {
            foreach (RelationProperty property in properties)
            {
                var queryable = (IQueryable)context.GetDbSet(property.RelationType);

                switch (property.RelationPropertyType)
                {
                case RelationPropertyType.Entity:
                    var refEntity = (IEntity)entity.GetValue(property).GetValue();
                    switch (refEntity.EntityState)
                    {
                    case EntityState.Modified:
                        queryable.UpdateEntity(refEntity);
                        refEntity.SetState(EntityState.Unchanged);
                        break;
                    }

                    HandleRelationProperties(refEntity);
                    break;

                case RelationPropertyType.EntitySet:
                    var value = entity.GetValue(property);
                    if (PropertyValue.IsEmpty(value))
                    {
                        value = entity.GetOldValue(property);
                    }

                    if (!PropertyValue.IsEmpty(value))
                    {
                        HandleRelationEntitySet(queryable, entity, value.GetValue() as IEntitySet, property);
                    }

                    break;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 为指定的类型返回 <see cref="EntityRepository&lt;TEntity&gt;"/>
        /// </summary>
        /// <typeparam name="TEntity">实体类型。</typeparam>
        /// <returns></returns>
        public EntityRepository <TEntity> Set <TEntity>() where TEntity : IEntity
        {
            Type entitytype = typeof(TEntity);

            return(context.GetDbSet(entitytype) as EntityRepository <TEntity>);
        }