/// <summary> /// 注册特殊的实体属性,这类属性为附加自实体间关系的不可持久化的属性。 /// </summary> /// <typeparam name="TEntity">实体的类型。</typeparam> /// <param name="expression">指定注册的属性的表达式。</param> /// <param name="referenceProperty">参数或引用的属性。</param> /// <param name="options">关联选项。</param> /// <returns>一个 <see cref="IProperty"/> 对象。</returns> public static IProperty RegisterSupposedProperty <TEntity>(Expression <Func <TEntity, object> > expression, IProperty referenceProperty = null, RelationOptions options = null) where TEntity : IEntity { var propertyInfo = PropertySearchVisitor.FindProperty(expression); if (propertyInfo == null) { throw new InvalidOperationException(SR.GetString(SRKind.InvalidRegisterExpression)); } return(RegisterSupposedProperty(propertyInfo, typeof(TEntity), referenceProperty, options)); }
/// <summary> /// 注册特殊的实体属性,这类属性为附加自实体间关系的不可持久化的属性。 /// </summary> /// <param name="propertyName">属性名称。</param> /// <param name="propertyType">属性类型。</param> /// <param name="entityType">实体类型。</param> /// <param name="referenceProperty">参数或引用的属性。</param> /// <param name="options">关联选项。</param> /// <returns>一个 <see cref="IProperty"/> 对象。</returns> public static IProperty RegisterSupposedProperty(string propertyName, Type propertyType, Type entityType, IProperty referenceProperty = null, RelationOptions options = null) { var propertyInfo = entityType.GetProperty(propertyName); if (propertyInfo == null) { throw new PropertyNotFoundException(propertyName); } return(RegisterSupposedProperty(propertyInfo, entityType, referenceProperty, options)); }
/// <summary> /// 注册特殊的实体属性,这类属性为附加自实体间关系的不可持久化的属性。 /// </summary> /// <param name="propertyInfo">属性名称。</param> /// <param name="entityType">实体类型。</param> /// <param name="referenceProperty">参数或引用的属性。</param> /// <param name="options">关联选项。</param> /// <returns>一个 <see cref="IProperty"/> 对象。</returns> private static IProperty RegisterSupposedProperty(PropertyInfo propertyInfo, Type entityType, IProperty referenceProperty = null, RelationOptions options = null) { IProperty property; var useAttr = propertyInfo.GetCustomAttributes <RelationshipUseAttribute>().FirstOrDefault(); if (referenceProperty != null) { if (referenceProperty.Type.IsEnum) { property = new EnumProperty { Name = propertyInfo.Name, Type = propertyInfo.PropertyType, EntityType = entityType, RelationalType = referenceProperty.Type, Reference = referenceProperty, Info = InitRelatedPropertyInfo(propertyInfo), Options = options ?? RelationOptions.Default }; } else { //引用属性 property = new ReferenceProperty { Name = propertyInfo.Name, Type = propertyInfo.PropertyType, EntityType = entityType, RelationalType = referenceProperty.EntityType, Reference = referenceProperty, Info = InitRelatedPropertyInfo(propertyInfo), Options = options ?? RelationOptions.Default }; } } else if (typeof(IEntity).IsAssignableFrom(propertyInfo.PropertyType)) { //实体引用属性 property = new EntityProperty { RelationalType = propertyInfo.PropertyType, Name = propertyInfo.Name, Type = propertyInfo.PropertyType, EntityType = entityType, RelationalKey = useAttr?.ForeignKey, Info = InitRelatedPropertyInfo(propertyInfo), Options = options ?? RelationOptions.Default }; } else if (propertyInfo.PropertyType.IsGenericType && typeof(IEntitySet).IsAssignableFrom(propertyInfo.PropertyType)) { //实体集属性 property = new EntitySetProperty { RelationalType = propertyInfo.PropertyType.GetGenericArguments()[0], Name = propertyInfo.Name, Type = propertyInfo.PropertyType, EntityType = entityType, RelationalKey = useAttr?.ForeignKey, Info = InitRelatedPropertyInfo(propertyInfo), Options = options ?? RelationOptions.Default }; } else { throw new NotImplementedException(); } return(RegisterProperty(entityType, property)); }