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(); } } } }
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(); } } } }
protected int InsertRows(string table_name) { DataTable myTable = this.Tables[table_name]; DataRowCollection rows = myTable.Rows; int rowsCount = rows.Count; int rowsAffected = 0; if (rowsCount <= 0) { return(rowsAffected); } var cmd = new InsertStatement(myTable.ExtendedProperties["Storage"].ToString()); cmd.ConnectionStringName = _connectionStringName; var prmlist = new List <SqlParameter>(); var colsCount = myTable.Columns.Count; var identcol = String.Empty; for (var c = 0; c < colsCount; c++) { var col = myTable.Columns[c]; var prm = new SqlParameter(col.ColumnName, col.DataType); prmlist.Add(prm); if (col.AutoIncrement) { identcol = col.ColumnName; } else if (col.ExtendedProperties["NonStorable"] == null) { cmd.Column(col.ExtendedProperties["Storage"].ToString(), prm); } } IEnumerator rowEnum = rows.GetEnumerator(); while (rowEnum.MoveNext()) { var row = (DataRow)rowEnum.Current; if (row.RowState != DataRowState.Added) { continue; } for (var c = 0; c < colsCount; c++) { var col = myTable.Columns[c]; var prm = prmlist[c]; if (row[prm.Name] == null || row[prm.Name] == DBNull.Value && col.DefaultValue != null) { prm.Value = col.DefaultValue; } else { prm.Value = row[prm.Name]; } } if (identcol != String.Empty) { object id; cmd.Execute(out id); row[identcol] = id;//Int32.Parse(id.ToString()); } else { cmd.Execute(); } row.AcceptChanges(); rowsAffected++; } return(rowsAffected); }