示例#1
0
        private void UpdateRelations(IEntity entity)
        {
            var entityEx = entity.As <IEntityStatefulExtension>();

            if (entityEx == null)
            {
                return;
            }

            var relProperties = PropertyUnity.GetRelatedProperties(entity.EntityType);

            foreach (var property in relProperties)
            {
                var relationPro = property as RelationProperty;
                var value       = entityEx.GetDirectValue(property);
                if (value == null)
                {
                    continue;
                }

                switch (relationPro.RelationPropertyType)
                {
                case RelationPropertyType.EntitySet:
                    var list = PropertyValueHelper.GetValue <IEnumerable>(value);
                    EntityUtility.AttachPrimaryKeyValues(entity, relationPro, list);
                    Save(list);
                    break;

                case RelationPropertyType.Entity:
                    SaveRelationEntity(PropertyValueHelper.GetValue <IEntity>(value));
                    break;
                }
            }
        }
 internal static IEnumerable <PropertyFieldMapping> GetDbMapping(Type entityType)
 {
     return(from s in PropertyUnity.GetLoadedProperties(entityType, true)
            let name = string.IsNullOrEmpty(s.Info.FieldName) ? s.Name : s.Info.FieldName
                       let dbType = s.Info.DataType == null ? DbType.String : s.Info.DataType.Value
                                    select new PropertyFieldMapping(s.Name, s.Info.FieldName, s.Type, dbType)
     {
         ValueFunc = e => PropertyValueHelper.GetValueSafely(((IEntity)e).InternalGetValue(s))
     });
 }
示例#3
0
        private PropertyValue GetSupportedValue(bool isNull, PropertyMapping mapper, IDataReader reader)
        {
            try
            {
                if (mapper.Property.Type == typeof(Guid))
                {
                    return(isNull ? new Guid?() : new Guid(reader.GetValue(mapper.Index).ToString()));
                }

                var typeCode = Type.GetTypeCode(mapper.Property.Type.GetNonNullableType());
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(isNull ? new bool?() : RecordWrapper.GetBoolean(reader, mapper.Index));

                case TypeCode.Char:
                    return(isNull ? new char?() : RecordWrapper.GetChar(reader, mapper.Index));

                case TypeCode.Byte:
                    return(isNull ? new byte?() : RecordWrapper.GetByte(reader, mapper.Index));

                case TypeCode.Int16:
                    return(isNull ? new short?() : RecordWrapper.GetInt16(reader, mapper.Index));

                case TypeCode.Int32:
                    return(isNull ? new int?() : RecordWrapper.GetInt32(reader, mapper.Index));

                case TypeCode.Int64:
                    return(isNull ? new long?() : RecordWrapper.GetInt64(reader, mapper.Index));

                case TypeCode.Decimal:
                    return(isNull ? new decimal?() : RecordWrapper.GetDecimal(reader, mapper.Index));

                case TypeCode.Double:
                    return(isNull ? new double?() : RecordWrapper.GetDouble(reader, mapper.Index));

                case TypeCode.String:
                    return(isNull ? string.Empty : RecordWrapper.GetString(reader, mapper.Index));

                case TypeCode.DateTime:
                    return(isNull ? new DateTime?() : RecordWrapper.GetDateTime(reader, mapper.Index));

                case TypeCode.Single:
                    return(isNull ? new float?() : RecordWrapper.GetFloat(reader, mapper.Index));

                default:
                    return(PropertyValueHelper.NewValue(RecordWrapper.GetValue(reader, mapper.Index)));
                }
            }
            catch (Exception ex)
            {
                throw new RowMapperCastException(mapper.Property.Name, mapper.Property.Type, ex);
            }
        }
示例#4
0
        private bool InternalCreate(IEntity entity)
        {
            var parameters = new ParameterCollection();
            var context    = CreateContext(parameters);
            var sql        = EntityPersistentQueryBuilder.BuidCreateQuery(context, entity);

            if (!sql.IsNullOrEmpty())
            {
                var rootType = entity.EntityType.GetRootType();

                //找出自增长序列的属性
                var incProperty = PropertyUnity.GetPersistentProperties(rootType).FirstOrDefault(s => s.Info.GenerateType == IdentityGenerateType.AutoIncrement);

                if (incProperty != null)
                {
                    if (!string.IsNullOrEmpty(context.Syntax.IdentitySelect))
                    {
                        //获得当前插入的自增长序列值
                        var identitySelect = context.Syntax.IdentitySelect;
                        if (!identitySelect.StartsWith(";"))
                        {
                            identitySelect = ";" + identitySelect;
                        }

                        var incValue = PropertyValueHelper.NewValue(context.Database.ExecuteScalar <int>(sql + identitySelect, context.Parameters));
                        entity.InternalSetValue(incProperty, incValue);
                        return(!incValue.IsNullOrEmpty());
                    }
                    else
                    {
                        //使用生成器生成值
                        var generator = context.Database.Provider.GetService <IGeneratorProvider>();
                        if (generator != null)
                        {
                            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);
                            var inc      = generator.GenerateValue(context.Database, context.Environment == null ? metadata.TableName : context.Environment.GetVariableTableName(metadata), incProperty.Info.FieldName);
                            entity.InternalSetValue(incProperty, inc);

                            parameters.Clear();
                            sql = EntityPersistentQueryBuilder.BuidCreateQuery(context, entity);

                            return(context.Database.ExecuteNonQuery(sql, context.Parameters) > 0);
                        }
                    }
                }

                return(database.ExecuteNonQuery(sql, parameters) > 0);
            }

            return(false);
        }
示例#5
0
        private PropertyValue GetSupportedValue(bool isNull, PropertyMapping mapper, DataRow row)
        {
            if (mapper.Property.Type == typeof(Guid))
            {
                return(isNull ? new Guid?() : new Guid(row[mapper.Index].ToString()));
            }

            var typeCode = Type.GetTypeCode(mapper.Property.Type.GetNonNullableType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return(isNull ? new bool?() : Convert.ToBoolean(row[mapper.Index]));

            case TypeCode.Char:
                return(isNull ? new char?() : Convert.ToChar(row[mapper.Index]));

            case TypeCode.Byte:
                return(isNull ? new byte?() : Convert.ToByte(row[mapper.Index]));

            case TypeCode.Int16:
                return(isNull ? new short?() : Convert.ToInt16(row[mapper.Index]));

            case TypeCode.Int32:
                return(isNull ? new int?() : Convert.ToInt32(row[mapper.Index]));

            case TypeCode.Int64:
                return(isNull ? new long?() : Convert.ToInt64(row[mapper.Index]));

            case TypeCode.Decimal:
                return(isNull ? new decimal?() : Convert.ToDecimal(row[mapper.Index]));

            case TypeCode.Double:
                return(isNull ? new double?() : Convert.ToDouble(row[mapper.Index]));

            case TypeCode.String:
                return(isNull ? string.Empty : Convert.ToString(row[mapper.Index]));

            case TypeCode.DateTime:
                return(isNull ? new DateTime?() : Convert.ToDateTime(row[mapper.Index]));

            case TypeCode.Single:
                return(isNull ? new float?() : Convert.ToSingle(row[mapper.Index]));

            default:
                return(PropertyValueHelper.NewValue(row[mapper.Index]));
            }
        }
示例#6
0
        private PropertyValue GetPropertyValue(PropertyMapping mapper, Func <int, bool> funcIsNull, Func <int, object> funcGetValue, Func <bool, PropertyValue> funcRead)
        {
            var isNull = funcIsNull(mapper.Index);

            if (mapper.Property.Type.IsArray &&
                mapper.Property.Type.GetElementType() == typeof(byte))
            {
                return(isNull ? new byte[0] : (byte[])funcGetValue(mapper.Index));
            }

            if (mapper.Property.Type.IsEnum)
            {
                return(GetEnumValue(mapper, isNull, funcGetValue));
            }

            if (PropertyValueHelper.IsSupportedType(mapper.Property.Type))
            {
                return(funcRead(isNull));
            }

            return(GetConvertedValue(mapper, isNull, funcGetValue));
        }
示例#7
0
 /// <summary>
 /// 获取指定属性的值。
 /// </summary>
 /// <typeparam name="T">值的类型。</typeparam>
 /// <param name="property">实体属性。</param>
 /// <returns></returns>
 public virtual T GetValue <T>(IProperty property)
 {
     return(PropertyValueHelper.GetValue <T>(GetValue(property)));
 }
示例#8
0
 /// <summary>
 /// 设置指定属性的值。
 /// </summary>
 /// <typeparam name="T">值的类型。</typeparam>
 /// <param name="property">实体属性。</param>
 /// <param name="value">要设置的值。</param>
 public virtual void SetValue <T>(IProperty property, T value)
 {
     SetValue(property, PropertyValueHelper.NewValue(value, property.Type));
 }