public override int Delete <TEntity>(TEntity entity, string table) { PublicHelper.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity)); PublicHelper.EnsureHasPrimaryKey(typeDescriptor); Dictionary <PropertyDescriptor, object> keyValueMap = new Dictionary <PropertyDescriptor, object>(typeDescriptor.PrimaryKeys.Count); foreach (PropertyDescriptor keyPropertyDescriptor in typeDescriptor.PrimaryKeys) { object keyVal = keyPropertyDescriptor.GetValue(entity); keyValueMap.Add(keyPropertyDescriptor, keyVal); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); PropertyDescriptor timestampProperty = typeDescriptor.PropertyDescriptors.Where(a => a.IsTimestamp()).FirstOrDefault(); if (timestampProperty != null) { object timestampValue = timestampProperty.GetValue(entity); if (timestampValue != null) { keyValueMap[timestampProperty] = timestampValue; } } DbExpression conditionExp = PrimaryKeyHelper.MakeCondition(keyValueMap, dbTable); DbDeleteExpression e = new DbDeleteExpression(dbTable, conditionExp); return(this.ExecuteNonQuery(e)); }
public override int Update <TEntity>(TEntity entity, string table) { PublicHelper.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity)); PublicHelper.EnsureHasPrimaryKey(typeDescriptor); Dictionary <PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor); IEntityState entityState = this.TryGetTrackedEntityState(entity); Dictionary <PropertyDescriptor, DbExpression> updateColumns = new Dictionary <PropertyDescriptor, DbExpression>(); foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.PropertyDescriptors) { if (keyValueMap.ContainsKey(propertyDescriptor)) { keyValueMap[propertyDescriptor] = propertyDescriptor.GetValue(entity); continue; } bool hasSequence = propertyDescriptor.HasSequence(); if (hasSequence) { continue; } object val = propertyDescriptor.GetValue(entity); if (entityState != null && !entityState.HasChanged(propertyDescriptor, val)) { continue; } DbExpression valExp = DbExpression.Parameter(val, propertyDescriptor.PropertyType, propertyDescriptor.Column.DbType); updateColumns.Add(propertyDescriptor, valExp); } if (updateColumns.Count == 0) { return(0); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); DbExpression conditionExp = PrimaryKeyHelper.MakeCondition(keyValueMap, dbTable); DbUpdateExpression e = new DbUpdateExpression(dbTable, conditionExp); foreach (var item in updateColumns) { e.UpdateColumns.Add(item.Key.Column, item.Value); } int ret = this.ExecuteNonQuery(e); if (entityState != null) { entityState.Refresh(); } return(ret); }
public virtual int Delete <TEntity>(TEntity entity, string table) { PublicHelper.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity)); PublicHelper.EnsureHasPrimaryKey(typeDescriptor); Dictionary <PropertyDescriptor, object> keyValueMap = new Dictionary <PropertyDescriptor, object>(typeDescriptor.PrimaryKeys.Count); foreach (PropertyDescriptor keyPropertyDescriptor in typeDescriptor.PrimaryKeys) { object keyVal = keyPropertyDescriptor.GetValue(entity); keyValueMap.Add(keyPropertyDescriptor, keyVal); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); DbExpression conditionExp = PrimaryKeyHelper.MakeCondition(keyValueMap, dbTable); DbDeleteExpression e = new DbDeleteExpression(dbTable, conditionExp); return(this.ExecuteNonQuery(e)); }
public override int Update <TEntity>(TEntity entity, string table) { PublicHelper.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity)); PublicHelper.EnsureHasPrimaryKey(typeDescriptor); Dictionary <PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor); IEntityState entityState = this.TryGetTrackedEntityState(entity); Dictionary <PropertyDescriptor, DbExpression> updateColumns = new Dictionary <PropertyDescriptor, DbExpression>(); PropertyDescriptor timestampProperty = null; object timestampValue = null; foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.PropertyDescriptors) { if (propertyDescriptor.IsPrimaryKey) { keyValueMap[propertyDescriptor] = propertyDescriptor.GetValue(entity); continue; } if (propertyDescriptor.IsAutoIncrement) { continue; } if (propertyDescriptor.IsTimestamp()) { timestampProperty = propertyDescriptor; timestampValue = propertyDescriptor.GetValue(entity); continue; } object val = propertyDescriptor.GetValue(entity); if (entityState != null && !entityState.HasChanged(propertyDescriptor, val)) { continue; } DbExpression valExp = DbExpression.Parameter(val, propertyDescriptor.PropertyType, propertyDescriptor.Column.DbType); updateColumns.Add(propertyDescriptor, valExp); } if (updateColumns.Count == 0) { return(0); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); if (timestampValue != null) { keyValueMap[timestampProperty] = timestampValue; } DbExpression conditionExp = PrimaryKeyHelper.MakeCondition(keyValueMap, dbTable); DbUpdateExpression e = new DbUpdateExpression(dbTable, conditionExp); foreach (var item in updateColumns) { e.UpdateColumns.Add(item.Key.Column, item.Value); } int rowsAffected = 0; if (timestampValue == null) { rowsAffected = this.ExecuteNonQuery(e); if (entityState != null) { entityState.Refresh(); } return(rowsAffected); } List <Action <TEntity, IDataReader> > mappers = new List <Action <TEntity, IDataReader> >(); mappers.Add(GetMapper <TEntity>(timestampProperty, e.Returns.Count)); e.Returns.Add(timestampProperty.Column); IDataReader dataReader = this.ExecuteReader(e); using (dataReader) { while (dataReader.Read()) { rowsAffected++; foreach (var mapper in mappers) { mapper(entity, dataReader); } } } return(rowsAffected); }