public bool Insert(TEntity entity) { var sql = sqlGenerator.Insert(); var newId = dataContext.ExecuteScalar <TEntity>(sql, entity.ToSqlParameters <TEntity>()); var mapping = EntityMappingEngine.GetMappingForEntity <TEntity>(); var propMapping = mapping?.PropertyMappings.FirstOrDefault(x => x.IsPrimaryKey); propMapping?.PropertyInfo.SetValue(entity, Convert.ChangeType(newId, propMapping.PropertyInfo.PropertyType), null); return(true); }
public static List <SqlParameter> ToSqlParameters <TEntity>(this object source) where TEntity : class { var list = new List <SqlParameter>(); var mapping = EntityMappingEngine.GetMappingForEntity <TEntity>(); foreach (var prop in source.GetType().GetProperties()) { var propMapping = mapping.GetMappingForType(prop); if (propMapping.IsIgnored) { continue; } list.Add(new SqlParameter(prop.Name, prop.GetValue(source, null))); } return(list); }
public static List <TEntity> MapTo <TEntity>(this IDataReader dr) where TEntity : class, new() { var list = new List <TEntity>(); while (dr.Read()) { var obj = Activator.CreateInstance <TEntity>(); try { var mapping = EntityMappingEngine.GetMappingForEntity <TEntity>(); foreach (var prop in obj.GetType().GetProperties()) { var propMapping = mapping.GetMappingForType(prop); if (propMapping.IsIgnored) { continue; } var colName = prop.Name; if (!HasColumn(dr, colName)) { continue; } if (!Equals(dr[colName], DBNull.Value)) { prop.SetValue(obj, dr[colName]); } } list.Add(obj); } catch (Exception ex) { throw; } } return(list); }
public SqlCrudGenerator() { EntityMapping = EntityMappingEngine.GetMappingForEntity <TEntity>(); }
public LambdaToSql() { EntityMapping = EntityMappingEngine.GetMappingForEntity <TEntity>(); }