/// <summary> /// Ctor, will transform a EF entity to a viewmodel /// </summary> /// <param name="p"></param> public PersonViewModel(Person p) : base(p) { this.Age = p.Age; this.Name = p.Name; this.Gender = p.GenderValue; this.FavoriteColorId = p.FavoriteColorId; this.FavoriteFruitId = p.FavoriteFruitId; if(p.FavoriteColor != null) this.FavoriteColor = new ColorViewModel(p.FavoriteColor); if (p.FavoriteFruit != null) this.FavoriteFruit = new FruitViewModel(p.FavoriteFruit); }
public void PersonDelete(Person entity) { try { var Db = this.getContext(); var dbEntity = Db.Set<Person>().Find(entity.Id); var p = Db.People.Remove(dbEntity); Db.SaveChanges(); //Notify all clients listening for this event that a Person was deleted! this.SendToAll(entity, Commands.PersonTrigger.Deleted); } catch (Exception ex) { this.DispatchError(ex, "Exception in PersonDelete"); } }
public void PersonCreate(Person entity) { try { var Db = this.getContext(); var p = Db.People.Add(entity); Db.SaveChanges(); //Get from DB since we might miss the Fruit/Color entity due to only setting FK´s on them. p.FavoriteFruit = Db.Set<Fruit>().Find(p.FavoriteFruitId); p.FavoriteColor = Db.Set<Color>().Find(p.FavoriteColorId); var json = new PersonViewModel(p); //Notify all clients listening for this event that a new Person was created! this.SendToAll(json, Commands.PersonTrigger.Created); } catch (Exception ex) { this.DispatchError(ex, "Exception in PersonCreate"); } }
public void PersonUpdate(Person entity) { try { var Db = this.getContext(); var dbEntity = Db.Set<Person>().Find(entity.Id); dbEntity.GenderValue = entity.GenderValue; dbEntity.Name = entity.Name; dbEntity.FavoriteColorId = entity.FavoriteColorId; dbEntity.FavoriteFruitId = entity.FavoriteFruitId; Db.Entry(dbEntity).State = EntityState.Modified; Db.SaveChanges(); entity.FavoriteFruit = Db.Set<Fruit>().Find(entity.FavoriteFruitId); entity.FavoriteColor = Db.Set<Color>().Find(entity.FavoriteColorId); var json = new PersonViewModel(entity); //Notify all clients listening for this event that a Person was updated! this.SendToAll(json, Commands.PersonTrigger.Updated); } catch (Exception ex) { this.DispatchError(ex, "Exception in PersonUpdate"); } }