Пример #1
0
        /// <summary>
        /// 处理实体的关联的属性。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="properties"></param>
        private async Task HandleRelationPropertiesAsync(IEntity entity, IEnumerable <IProperty> properties, CancellationToken cancellationToken = default)
        {
            foreach (RelationProperty property in properties)
            {
                var queryable = (IQueryable)context.GetDbSet(property.RelationalType);

                switch (property.RelationalPropertyType)
                {
                case RelationPropertyType.Entity:
                    var refEntity = (IEntity)entity.GetValue(property).GetValue();
                    switch (refEntity.EntityState)
                    {
                    case EntityState.Modified:
                        await queryable.UpdateEntityAsync(refEntity, cancellationToken);

                        refEntity.SetState(EntityState.Unchanged);
                        break;
                    }

                    await HandleRelationPropertiesAsync(refEntity, cancellationToken);

                    break;

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

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

                    break;
                }
            }
        }
Пример #2
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.RelationalType);

                switch (property.RelationalPropertyType)
                {
                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;
                }
            }
        }