public virtual void Insert(TEntity entity) { var insert = SqlUtils.CreateInsertStatement(_metadata, IdentityInsert); insert.ConnectionStringName = _connectionStringName; foreach (var item in _metadata.Columns) { if (IdentityInsert || !item.IsDbGenerated) { insert.SetParamValue(item.Name, entity[item.Name]); } } if (!String.IsNullOrEmpty(insert.IdentityColumnName)) { object id; insert.Execute(out id); entity[insert.IdentityColumnName] = Int32.Parse(id.ToString()); } else { insert.Execute(); } if (_hasManyAssociations.Count() == 0) { return; } foreach (IAssociationMapping hma in _hasManyAssociations) { var items = entity[hma.Name] as IEnumerable <IIndexable>; if (items == null || items.Count() == 0) { continue; } var omapping = MappingFactory.GetMapping(hma.OtherMappingType); InsertStatement insert2 = SqlUtils.CreateInsertStatement(omapping); insert2.ConnectionStringName = insert.ConnectionStringName; int colCount = omapping.Columns.Count; foreach (TEntity item in items) { if (!String.IsNullOrEmpty(insert.IdentityColumnName)) { item[hma.OtherKeys[0]] = entity[insert.IdentityColumnName]; } for (var x = 0; x < colCount; x++) { var propName = omapping.Columns[x].Name; insert2.SetParamValue(propName, item[propName]); } if (!String.IsNullOrEmpty(insert2.IdentityColumnName)) { object id; insert2.Execute(out id); item[insert2.IdentityColumnName] = Int32.Parse(id.ToString()); } else { insert2.Execute(); } } } }
public virtual void Update(TEntity entity) { ICondition criteria = IdentityInsert ? Conditions.IsEqual(Entity.ID, new SqlNumber(entity.Id.ToString())) : Conditions.IsEqual(Entity.ID, new SqlString(entity.Id.ToString())); var current = GetOne(criteria); if (current == null) { return; } bool hasChanges = false; foreach (var item in _metadata.Columns) { if (current[item.Name] != entity[item.Name]) { hasChanges = true; break; } } if (hasChanges) { var update = SqlUtils.CreateUpdateStatement(_metadata); update.ConnectionStringName = _connectionStringName; foreach (var item in _metadata.Columns) { update.SetParamValue(item.Name, entity[item.Name]); } update.Execute(); } if (_hasManyAssociations.Count() == 0) { return; } foreach (IAssociationMapping hma in _hasManyAssociations) { var items = entity[hma.Name] as IEnumerable <ValueObject>; if (items == null) { continue; } var storedItems = current[hma.Name] as IEnumerable <ValueObject>; // if (storedItems == null) // continue; var omapping = MappingFactory.GetMapping(hma.OtherMappingType); //var primKeys = omapping.Columns.Where(o => o.IsPrimaryKey || o.IsDbGenerated).ToList(); DeleteStatement deleteStament = SqlUtils.CreateDeleteStatement(omapping, true); deleteStament.ConnectionStringName = _connectionStringName; InsertStatement insertStament = SqlUtils.CreateInsertStatement(omapping); insertStament.ConnectionStringName = _connectionStringName; if (storedItems != null) { // Delete non existent foreach (ValueObject storedItem in storedItems) { var itemFound = items.FirstOrDefault(ci => ci.Equals(storedItem)); if (itemFound == null) { foreach (ColumnMapping cm in omapping.Columns) { deleteStament.SetParamValue(cm.Name, storedItem[cm.Name]); } deleteStament.Execute(); } } // Insert non existent foreach (ValueObject item in items) { var itemFound = storedItems.FirstOrDefault(si => si.Equals(item)); if (itemFound == null) { foreach (ColumnMapping cm in omapping.Columns) { insertStament.SetParamValue(cm.Name, item[cm.Name]); } insertStament.Execute(); } } } else { foreach (ValueObject item in items) { foreach (ColumnMapping cm in omapping.Columns) { insertStament.SetParamValue(cm.Name, item[cm.Name]); } insertStament.Execute(); } } } }