public void HaveUserResolveConcurrency(DbPropertyValues currentValues, DbPropertyValues databaseValues, DbPropertyValues resolvedValues) { currentValues.GetValue <Article>("ArticleId"); resolvedValues.SetValues(db.Articles.Find().Name); }
public override List <DocumentKey> GetRelatedEntityDocumentKeysOfModifiedEntity(SimpleEntity updatedOrDeletedEntity, DbPropertyValues originalValues) { var id = originalValues.GetValue <int>(PropertyHelper.GetPropertyName <SimpleEntity>(x => x.Id)); var list = new List <DocumentKey>(); list.Add(GetDocumentKey(id)); return(list); }
public DbEntityPropertyValues(DbPropertyValues dbPropertyValues) { IEnumerable <string> dbPropertyNames = dbPropertyValues.PropertyNames; foreach (var prop in dbPropertyNames) { entityKeyValuePairs.Add(prop, dbPropertyValues.GetValue <Object>(prop)); } }
private object GetProperty(DbPropertyValues dbValues, string propName) { if (dbValues == null) { return(""); } else { return(dbValues.GetValue <object>(propName)); } }
public int Add(T entity) { using (context = new ReservationContext()) { context.Set <T>().Add(entity); context.SaveChanges(); DbPropertyValues sa = context.Entry(entity).GetDatabaseValues(); string keyName = sa.PropertyNames.First(); int Id = sa.GetValue <int>(keyName); return(Id); } }
/// <summary> /// Creates the entity with values. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="values">The values.</param> /// <returns>T.</returns> public static T CreateEntityWithValues <T>(DbPropertyValues values) where T : new() { T entity = new T(); Type type = typeof(T); foreach (var name in values.PropertyNames) { var property = type.GetProperty(name); property.SetValue(entity, values.GetValue <object>(name)); } return(entity); }
/// <summary> /// Returns the organization document keys from the added address. /// </summary> /// <param name="originalValues">The address original values.</param> /// <returns>The organization document keys.</returns> public List <DocumentKey> GetOrganizationDocumentKeys(DbPropertyValues originalValues) { Contract.Requires(originalValues != null, "The original values must not be null."); var organizationId = originalValues.GetValue <int?>(ORGANIZATION_ID_PROPERTY_NAME); var list = new List <DocumentKey>(); if (organizationId.HasValue) { list.Add(GetOrganizationDocumentKey(organizationId.Value)); } return(list); }
public void TestEstadoEntidades(int idAlum, int idAsig) { using (bd_notasAlumnosEntities contextoDeObjs = new bd_notasAlumnosEntities()) { // Consulta para obtener la fila a modificar var consulta1 = from al_as in contextoDeObjs.alums_asigs where al_as.id_alumno == idAlum && al_as.id_asignatura == idAsig select al_as; // Ejecutar la consulta y cambiar la nota consulta1.First().nota = consulta1.First().nota + 0.5F; // Entidades modificadas var entidadesModificadas = from objDbEntityEntry in contextoDeObjs.ChangeTracker.Entries() where objDbEntityEntry.State == EntityState.Modified select objDbEntityEntry; // Valores originales y actuales foreach (var objDbEE in entidadesModificadas) { string info = "Tipo del objeto de seguimiento: " + objDbEE.ToString() + "\nID alumno: " + (objDbEE.Entity as alum_asig).id_alumno + "\nID asignatura: " + (objDbEE.Entity as alum_asig).id_asignatura + "\nValores originales -> actuales:\n"; DbPropertyValues actuales = objDbEE.CurrentValues; DbPropertyValues originales = objDbEE.OriginalValues; int c = actuales.PropertyNames.Count(); for (int i = 0; i < c; i++) { string prop = actuales.PropertyNames.ElementAt(i); info += "\t" + originales.GetValue <object>(prop) + " -> " + actuales.GetValue <object>(prop) + "\n"; } MessageBox.Show(info); } // Salvar los cambios en la base de datos try { contextoDeObjs.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
protected virtual object OriginalValue(string propertyName) { object originalValue = null; if (GlobalTrackingConfig.DisconnectedContext) { if (_dbEntryDbValues == null) { _dbEntryDbValues = DbEntry.GetDatabaseValues(); } originalValue = _dbEntryDbValues.GetValue <object>(propertyName); } else { originalValue = DbEntry.Property(propertyName).OriginalValue; } return(originalValue); }
private static object CreateWithValues(DbPropertyValues values, Type type = null) { if (type == null) { type = typeof(T); } var entity = Activator.CreateInstance(type); foreach (string name in values.PropertyNames) { var value = values.GetValue <object>(name); var property = type.GetProperty(name); if (value != null) { var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; property.SetValue(entity, Convert.ChangeType(value, propertyType), null); } } return(entity); }
public TValue GetValue <TValue>(string key) { return(_wrappedValues.GetValue <TValue>(key)); }
public override int SaveChanges() { //int result = -1; DateTime eventDateTime = DateTime.Now; //string eventUser = HttpContext.Current.User.Identity.Name; User u = GetUserByLoginName(HttpContext.Current.User.Identity.Name.Split('\\').Last(), false); string eventUser = u.DisplayName; ChangeTracker.DetectChanges(); // Important! ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext; List <ObjectStateEntry> objectStateEntryList = ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted) .ToList(); foreach (ObjectStateEntry entry in objectStateEntryList) { AuditEvent auditRecord = new AuditEvent(); auditRecord.EventDate = DateTime.Now; auditRecord.UserID = eventUser; if (entry.Entity == null) { continue; } string objModel = entry.Entity.ToString(); string[] objArray = objModel.Split('.'); string objType = objArray.Last();; if (objType.Contains("_")) { objType = objType.Substring(0, objType.IndexOf('_')); var testAuditType = this.AuditDescriptions.Where(a => a.AuditDescription.StartsWith(objType)); if (testAuditType.Count() == 0) { objType = entry.EntitySet.ToString(); } else { string newObjType = testAuditType.First().AuditDescription.ToString(); int spaceLoc = newObjType.IndexOf(' '); if (spaceLoc > 0) { objType = newObjType.Substring(0, spaceLoc); } } } if (!entry.IsRelationship && !objType.StartsWith("Audit") && objType != "EdmMetadata") { switch (entry.State) { case EntityState.Added: { //result = base.SaveChanges(); string objName = string.Format("{0} Added", objType); int AuditType = this.AuditDescriptions.Where(a => a.AuditDescription.ToLower() == objName.ToLower()).Take(1).Single().idAuditEventDescription; auditRecord.EventDescription = objName; auditRecord.idAuditEventDescription = AuditType; auditRecord.RecordChanged = entry.CurrentValues.GetValue(0).ToString(); try { int ord = entry.CurrentValues.GetOrdinal("tipstaffRecordID"); string value = entry.CurrentValues.GetValue(ord).ToString(); auditRecord.RecordAddedTo = Int32.Parse(value); } catch { try { int ord = entry.CurrentValues.GetOrdinal("warrantID"); string value = entry.CurrentValues.GetValue(ord).ToString(); auditRecord.RecordAddedTo = Int32.Parse(value); } catch { try { int ord = entry.CurrentValues.GetOrdinal("childAbductionID"); string value = entry.CurrentValues.GetValue(ord).ToString(); auditRecord.RecordAddedTo = Int32.Parse(value); } catch { auditRecord.RecordAddedTo = null; } } } break; } case EntityState.Deleted: { string objName = string.Format("{0} Deleted", objType); int AuditType = this.AuditDescriptions.Where(a => a.AuditDescription.ToLower() == objName.ToLower()).Take(1).Single().idAuditEventDescription; auditRecord.EventDescription = objName; auditRecord.idAuditEventDescription = AuditType; auditRecord.RecordChanged = entry.OriginalValues.GetValue(0).ToString(); try { int ord = entry.OriginalValues.GetOrdinal("tipstaffRecordID"); string value = entry.OriginalValues.GetValue(ord).ToString(); auditRecord.RecordAddedTo = Int32.Parse(value); } catch { try { int ord = entry.OriginalValues.GetOrdinal("warrantID"); string value = entry.OriginalValues.GetValue(ord).ToString(); auditRecord.RecordAddedTo = Int32.Parse(value); } catch { try { int ord = entry.OriginalValues.GetOrdinal("childAbductionID"); string value = entry.OriginalValues.GetValue(ord).ToString(); auditRecord.RecordAddedTo = Int32.Parse(value); } catch { auditRecord.RecordAddedTo = null; } } } // Iterate over the members (i.e. properties (including complex properties), references, collections) of the entity type List <AuditEventDataRow> data = new List <AuditEventDataRow>(); foreach (EdmMember member in entry.EntitySet.ElementType.Members) { string propertyName = member.Name.ToString(); DbPropertyValues oldData = this.Entry(entry.Entity).GetDatabaseValues(); string oldValue = ""; string newValue = "deleted"; try { oldValue = (oldData.GetValue <object>(propertyName) != null) ? oldData.GetValue <object>(propertyName).ToString() : "Empty"; if (oldValue == "") { oldValue = "Empty"; } } catch { oldValue = "Could not be mapped"; } if ((oldValue != newValue) && (oldValue != "Could not be mapped")) // probably not necessary { AuditEventDataRow newAuditRow = new AuditEventDataRow(); newAuditRow.ColumnName = propertyName; newAuditRow.Was = oldValue.Length <= 199 ? oldValue : oldValue.Substring(0, 199); newAuditRow.Now = newValue.Length <= 199 ? newValue : newValue.Substring(0, 199); data.Add(newAuditRow); } } if (data.Count() > 0) { auditRecord.AuditEventDataRows = data; } break; } case EntityState.Modified: { string objName = string.Format("{0} Amended", objType); int AuditType = this.AuditDescriptions.Where(a => a.AuditDescription.ToLower() == objName.ToLower()).Take(1).Single().idAuditEventDescription; auditRecord.EventDescription = objName; auditRecord.idAuditEventDescription = AuditType; auditRecord.RecordChanged = entry.CurrentValues.GetValue(0).ToString(); List <AuditEventDataRow> data = new List <AuditEventDataRow>(); foreach (string propertyName in entry.GetModifiedProperties()) { DbPropertyValues oldData = this.Entry(entry.Entity).GetDatabaseValues(); string oldValue = (oldData.GetValue <object>(propertyName) != null) ? oldData.GetValue <object>(propertyName).ToString() : "Empty"; if (oldValue == "") { oldValue = "Empty"; } CurrentValueRecord current = entry.CurrentValues; string newValue = (current.GetValue(current.GetOrdinal(propertyName)) != null) ? current.GetValue(current.GetOrdinal(propertyName)).ToString() : "Empty"; if (newValue == "") { newValue = "Empty"; } if (objType == "Template" && propertyName == "templateXML") { oldValue = "XML"; newValue = "XML - Too long to record new version"; } if (oldValue != newValue) // probably not necessary { AuditEventDataRow newAuditRow = new AuditEventDataRow(); newAuditRow.ColumnName = propertyName; newAuditRow.Was = oldValue.Length <= 199 ? oldValue : oldValue.Substring(0, 199); newAuditRow.Now = newValue.Length <= 199 ? newValue : newValue.Substring(0, 199); data.Add(newAuditRow); } } if (data.Count() > 0) { auditRecord.AuditEventDataRows = data; } break; } } } if (auditRecord.RecordChanged == "0" && auditRecord.RecordAddedTo == 0 && auditRecord.EventDescription.Contains("Added")) { //New TipstaffRecord derivative record added, so... //save the record base.SaveChanges(); //extract the new identity auditRecord.RecordChanged = entry.CurrentValues.GetValue(0).ToString(); //update the audit event this.AuditEvents.Add(auditRecord); //and savechanges at the end of the code block } else if (auditRecord.RecordChanged == "0" && auditRecord.RecordAddedTo != 0 && auditRecord.EventDescription.Contains("Added")) { //New record added, so... //save the record base.SaveChanges(); //extract the new identity auditRecord.RecordChanged = entry.CurrentValues.GetValue(0).ToString(); //update the audit event this.AuditEvents.Add(auditRecord); //and savechanges at the end of the code block } else if (auditRecord.RecordChanged != "0" && auditRecord.RecordChanged != null && (auditRecord.AuditEventDataRows != null && auditRecord.AuditEventDataRows.Count > 0)) { this.AuditEvents.Add(auditRecord); //base.SaveChanges(); } try { //base.SaveChanges(); //only uncomment for error handling } catch (DbEntityValidationException ex) { System.Diagnostics.Debug.Print(ex.Message); } catch (DbUpdateException ex) { System.Diagnostics.Debug.Print(ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.Print(ex.Message); } } return(base.SaveChanges()); }
public TValue GetValue <TValue>(string propertyName) { return(_dbPropertyValues.GetValue <TValue>(propertyName)); }