/// <summary> /// 内部设置属性值的方法。 /// </summary> /// <param name="property"></param> /// <param name="value"></param> private void InternalSetValue(IProperty property, PropertyValue value) { if (PropertyValue.IsEmpty(value)) { if (SetNullable(property)) { return; } } if (property is RelationProperty) { InnerLazyMgr.SetValueCreated(property.Name); } InnerEntry.Modify(property.Name, value); }
/// <summary> /// 处理附加属性的值。 /// </summary> /// <param name="property"></param> /// <returns></returns> private PropertyValue ProcessSupposedProperty(IProperty property) { if (property is RelationProperty relationProperty && relationProperty.Options.LoadBehavior != LoadBehavior.None) { var value = EntityLazyloader.Load(this, relationProperty); if (value != null && !PropertyValue.IsEmpty(value)) { InnerLazyMgr.SetValueCreated(property.Name); value._dataType = property.Info.DataType; InnerEntry.Initializate(property.Name, value); } return(value); } return(PropertyValue.Empty); }
/// <summary> /// 检查关联属性的空值,如果属性值已设置为null,则不应显示给客户端。 /// </summary> /// <param name="property">要检查的属性。</param> /// <param name="value">属性的值。</param> /// <returns></returns> internal static PropertyValue CheckReturnValue(IProperty property, PropertyValue value) { var relationPro = property.As <RelationProperty>(); if (relationPro == null || PropertyValue.IsEmpty(value)) { return(value); } switch (relationPro.RelationalPropertyType) { case RelationPropertyType.Entity: return(CheckReturnEntityValue(value.GetValue().As <IEntity>(), value)); case RelationPropertyType.EntitySet: return(CheckReturnEntitySetValue(value.GetValue() as IEntitySet, value)); default: return(value); } }
/// <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; } } }
/// <summary> /// 使用所添加的变量解析实体映射的数据表名称。 /// </summary> /// <param name="entityType">实体类型。</param> /// <param name="entity">当前实体</param> /// <returns></returns> public string GetVariableTableName(Type entityType, IEntity entity) { Guard.ArgumentNull(entityType, "entityType"); var metadata = EntityMetadataUnity.GetEntityMetadata(entityType); var regx = new Regex(@"(<\w+>)"); var matches = regx.Matches(metadata.TableName); if (matches.Count == 0) { return(metadata.TableName); } var tableName = metadata.TableName; var flag = new AssertFlag(); foreach (Match match in matches) { var key = match.Value.TrimStart('<').TrimEnd('>'); object v; if (parameters.TryGetValue(key, out v)) { v.As <IProperty>(p => { var value = entity.GetValue(p); if (!PropertyValue.IsEmpty(value)) { flag.AssertTrue(); tableName = tableName.Replace(match.Value, value.ToString()); } }); } } if (flag.AssertTrue()) { return(GetVariableTableName(metadata)); } return(tableName); }
/// <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; } } }
/// <summary> /// 判断属性是否需要纠正默认值的类型。 /// </summary> /// <param name="property"></param> /// <returns></returns> private static bool IsNeedCorrectDefaultValue(IProperty property) { return(!PropertyValue.IsEmpty(property.Info.DefaultValue) && (property.Type.IsEnum || property.Type == typeof(bool) || property.Type == typeof(bool?))); }
/// <summary> /// 检查实体的关联属性。 /// </summary> /// <param name="entity"></param> private void HandleRelationProperties(IEntity entity) { //判断实体类型有是不是编译的代理类型,如果不是,取非null的属性,否则使用IsModified判断 var isNotCompiled = entity.GetType().IsNotCompiled(); var properties = PropertyUnity.GetRelatedProperties(entity.EntityType).Where(m => isNotCompiled ? !PropertyValue.IsEmpty(entity.GetValue(m)) : entity.IsModified(m.Name)); HandleRelationProperties(entity, properties); }
/// <summary> /// 检查有没有关联属性被修改. /// </summary> /// <param name="entity"></param> /// <returns></returns> private bool CheckRelationHasModified(IEntity entity) { //判断实体类型有是不是编译的代理类型,如果不是,取非null的属性,否则使用IsModified判断 var isNotCompiled = entity.GetType().IsNotCompiled(); return(PropertyUnity.GetRelatedProperties(entity.EntityType).Any(s => isNotCompiled ? !PropertyValue.IsEmpty(entity.GetValue(s)) : entity.IsModified(s.Name))); }
/// <summary> /// 构造创建表的语句。 /// </summary> /// <param name="entityType">实体类型。</param> /// <param name="syntax">语法服务。</param> /// <param name="tableName">数据表名称。</param> /// <returns></returns> private static string BuildTableScript(Type entityType, ISyntaxProvider syntax, string tableName) { var sb = new StringBuilder(); sb.AppendFormat("create table {0}\n(\n", tableName); //获取实体类型中所有可持久化的属性,不包含引用类型的属性 var properties = PropertyUnity.GetPersistentProperties(entityType).ToArray(); var primaryPeoperties = PropertyUnity.GetPrimaryProperties(entityType).ToArray(); var count = properties.Length; for (var i = 0; i < count; i++) { var property = properties[i]; sb.AppendFormat(" {0}", property.Info.FieldName); //数据类型及长度精度等 sb.AppendFormat(" {0}", syntax.Column((DbType)property.Info.DataType, property.Info.Length, property.Info.Precision, property.Info.Scale)); //自增 if (property.Info.GenerateType == IdentityGenerateType.AutoIncrement && !string.IsNullOrEmpty(syntax.IdentityColumn)) { sb.AppendFormat(" {0}", syntax.IdentityColumn); } //不可空 if (!property.Info.IsNullable) { sb.AppendFormat(" not null"); } //默认值 if (!PropertyValue.IsEmpty(property.Info.DefaultValue)) { if (property.Type == typeof(string)) { sb.AppendFormat(" default '{0}'", property.Info.DefaultValue); } else if (property.Type.IsEnum) { sb.AppendFormat(" default {0}", (int)property.Info.DefaultValue); } else if (property.Type == typeof(bool) || property.Type == typeof(bool?)) { sb.AppendFormat(" default {0}", (bool)property.Info.DefaultValue ? 1 : 0); } else { sb.AppendFormat(" default {0}", property.Info.DefaultValue); } } if (i != count - 1) { sb.Append(","); } sb.AppendLine(); } //主键 if (primaryPeoperties.Length > 0) { sb.Append(","); sb.AppendFormat("constraint PK_{0} primary key (", tableName); for (var i = 0; i < primaryPeoperties.Length; i++) { if (i != 0) { sb.Append(","); } sb.Append(primaryPeoperties[i].Info.FieldName); } sb.Append(")"); } sb.Append(")\n"); return(sb.ToString()); }
/// <summary> /// 通过一组主键值返回一个实体对象。 /// </summary> /// <param name="primaryValues">一组主键值。</param> /// <returns></returns> public TEntity Get(PropertyValue[] primaryValues) { if (primaryValues.Any(s => PropertyValue.IsEmpty(s))) { return(default);
/// <summary> /// 读取实体实例的所有值。 /// </summary> /// <param name="instance">要读取的实体实例。</param> /// <returns>所有属性的值。</returns> public override IEnumerable <object> ReadValues(TEntity instance) { return(PropertyUnity.GetLoadedProperties(typeof(TEntity)) .Select(p => instance.GetValue(p)) .Select(value => PropertyValue.IsEmpty(value) ? null : value.GetValue())); }
/// <summary> /// 检查实体的关联属性。 /// </summary> /// <param name="entity"></param> private async Task HandleRelationPropertiesAsync(IEntity entity, CancellationToken cancellationToken = default) { //判断实体类型有是不是编译的代理类型,如果不是,取非null的属性,否则使用IsModified判断 var isNotCompiled = entity.GetType().IsNotCompiled(); var properties = PropertyUnity.GetRelatedProperties(entity.EntityType).Where(m => isNotCompiled ? !PropertyValue.IsEmpty(entity.GetValue(m)) : entity.IsModified(m.Name)); await HandleRelationPropertiesAsync(entity, properties, cancellationToken); }