/// <summary> /// Podívá se do prvního parametru (entityThatWasEdited) a poté veme hodnotu vlastnosti (changedPropertyName) /// a zrcadlí jí do databáze. Ostatní datové složky jsou zachovány. /// </summary> /// <param name="entityThatWasEdited"></param> /// <param name="changedPropertyName"></param> public void EditOnlySinglePropertyOfEntity(EntityNotify entityThatWasEdited, string changedPropertyName) { int propertyBeingEditedId = entityThatWasEdited.Id; Entity entityToEditFromDb = _database.Entities.Where(e => e.Id == propertyBeingEditedId).Single(); object editedPropertyValue = GetPropValue(entityThatWasEdited, changedPropertyName); SetProperty(entityToEditFromDb, changedPropertyName, editedPropertyValue); _database.SaveChanges(); System.Diagnostics.Debug.WriteLine(changedPropertyName + "saved!!!"); }
public void EditEntity(EntityNotify entityToEdit) { // Převedení EntityNotify na Db Entitu Entity converted = new Entity() { Id = entityToEdit.Id, Check = entityToEdit.Check, Date = entityToEdit.Date, Name = entityToEdit.Name }; Entity entityToEditFromDb = _database.Entities.Where(e => e.Id == converted.Id).Single(); _database.Entry(entityToEditFromDb).CurrentValues.SetValues(converted); _database.SaveChanges(); }
private void grid1_InitializingNewItem(object sender, InitializingNewItemEventArgs e) { MessageBox.Show("initializing new item"); EntityNotify itemCreatedByGrid = e.NewItem as EntityNotify; // Defaultní hodnoty pro nový řádek itemCreatedByGrid.Date = DateTime.Now; itemCreatedByGrid.Name = "Jméno"; itemCreatedByGrid.Check = false; itemCreatedByGrid.PropertyChanged += (obj, args) => { Configuration.DatabaseHelper.EditOnlySinglePropertyOfEntity(obj as EntityNotify, args.PropertyName); }; EntityNotify entityAlreadySavedInDb = Configuration.DatabaseHelper.AddNewEntity(itemCreatedByGrid); itemCreatedByGrid.Id = entityAlreadySavedInDb.Id; // Aktualizujeme Id dle databáze }
/// <summary> /// Přidá do databáze novou Entitu a nechá databázi, ať jí vygeneruje její unikátní Id. /// Metoda vrací poté tuto entitu zpět, i s daným ID. /// </summary> /// <param name="newEntity"></param> /// <returns></returns> public EntityNotify AddNewEntity(EntityNotify newEntity) { Entity converted = new Entity() { Check = newEntity.Check, Date = newEntity.Date, Name = newEntity.Name }; _database.Entities.Add(converted); _database.SaveChanges(); Entity entityWithDbGeneratedId = _database.Entities.Where(e => e.Id == converted.Id).Single(); return(new EntityNotify { Id = entityWithDbGeneratedId.Id, Check = entityWithDbGeneratedId.Check, Date = entityWithDbGeneratedId.Date, Name = entityWithDbGeneratedId.Name, }); }