/// <summary> /// 动态的填充一个实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="reader">只接收DataRow和SqlDataReader作为参数,使用其他类型的对象会发生错误</param> /// <param name="entity">实体类的实例,如果不使用此参数,就回创建一个新实例</param> /// <returns></returns> private static TEntity DynamicFillEntity <TEntity>(dynamic reader, TEntity entity = default(TEntity)) where TEntity : SQLEntity { List <ColumnContext> contexts; var entityType = typeof(TEntity); if (!fillEntityCache.TryGetValue(entityType, out contexts)) { contexts = new List <ColumnContext>(); foreach (var propertyInfo in entityType.GetProperties()) { if (EntityParser.IsIgnore(propertyInfo)) { continue; } var config = EntityParser.GetColumnConfig(propertyInfo); var context = new ColumnContext() { Config = config, PropertyInfo = propertyInfo, PropertyName = propertyInfo.Name, ColumnName = EntityParser.GetColumnName(propertyInfo) }; contexts.Add(context); } fillEntityCache.Add(entityType, contexts); } if (entity == null) { entity = Activator.CreateInstance <TEntity>(); } foreach (var context in contexts) { var value = reader[context.ColumnName]; FillProperty(entity, context.PropertyInfo, value); } entity.EntityState = EntityState.NotChanged; return(entity); }
/// <summary> /// 向数据库中插入一个实体并返回实体类的主键的值,并从数据库中刷新实体的所有值 /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <param name="entity">要插入到数据库的实体类的实例</param> /// <returns></returns> public static TEntity Insert <TEntity>(TEntity entity) where TEntity : SQLEntity { var entityType = typeof(TEntity); List <ColumnContext> contexts; if (!insertCache.TryGetValue(entityType, out contexts)) { contexts = new List <ColumnContext>(); foreach (var propertyInfo in entityType.GetProperties()) { if (EntityParser.IsIgnore(propertyInfo)) { continue; } var config = EntityParser.GetColumnConfig(propertyInfo); var context = new ColumnContext() { Config = config, PropertyInfo = propertyInfo, PropertyName = propertyInfo.Name, ColumnName = EntityParser.GetColumnName(propertyInfo) }; contexts.Add(context); } insertCache.Add(entityType, contexts); } List <SqlParameter> parameters = new List <SqlParameter>(); foreach (var context in contexts) { if (context.Config.UsePropertyForInsert) { var valueExpression = "@" + context.PropertyName; var propertyValue = context.PropertyInfo.GetValue(entity, null); if (context.Config.DBType == DBType.PropertyType) //如果直接用属性 { parameters.Add(new SqlParameter(valueExpression, propertyValue)); } else //如果要转换类型 { propertyValue = EntityParser.ConvertPropertyValue(context.PropertyInfo, propertyValue, context.Config.DBType); parameters.Add(new SqlParameter(valueExpression, propertyValue)); } } } var keyContext = EntityParser.GetKey(entityType); //寻找主键特性 object key; string sql = SqlBuilder.InsertSQL(entityType); if (keyContext.KeyConfig.UsePropertyForInsert) //如果使用属性的值插入到数据库 { SQLServerHelper.ExecuteNonQuery(sql, parameters.ToArray()); key = keyContext.Key.GetValue(entity, null); } else { key = SQLServerHelper.GetSingle(sql, parameters.ToArray()); //插入并获取主键的值 } var se = entity as SQLEntity; if (se != null) { se.EntityState = EntityState.NotInitialized; //未初始化 } string selectSQL = SqlBuilder.SelectSQL(typeof(TEntity), string.Format("{0} = @{0}", keyContext.PropertyName)); SQLServerHelper.FirstOrDefault((reader) => { return(FilldEntity <TEntity>(reader, entity)); }, selectSQL, new SqlParameter("@" + keyContext.PropertyName, key)); //从数据库中刷新实体 if (se != null) { se.EntityState = EntityState.NotChanged; //未修改 } return(entity); }