protected AddRemoveDetailEntitiesViewModel(IUnitOfWorkFactory <TUnitOfWork> unitOfWorkFactory, Func <TUnitOfWork, IRepository <TEntity, TPrimaryKey> > getRepositoryFunc, Func <TUnitOfWork, IRepository <TDetailEntity, TDetailPrimaryKey> > getDetailsRepositoryFunc, Func <TEntity, ICollection <TDetailEntity> > getDetailsFunc, TPrimaryKey id) : base(unitOfWorkFactory, getRepositoryFunc, null) { this.getDetailsRepositoryFunc = getDetailsRepositoryFunc; this.getDetailsFunc = getDetailsFunc; SelectedEntities = new ObservableCollection <TDetailEntity>(); if (this.IsInDesignMode()) { return; } LoadEntityByKey(id); Messenger.Default.Register(this, (EntityMessage <TDetailEntity, TDetailPrimaryKey> m) => { if (m.MessageType != EntityMessageType.Added) { return; } var withParent = m.Sender as ISupportParentViewModel; if (withParent == null || withParent.ParentViewModel != this) { return; } var withEntity = m.Sender as SingleObjectViewModel <TDetailEntity, TDetailPrimaryKey, TUnitOfWork>; var detailEntity = DetailsRepository.Find(DetailsRepository.GetPrimaryKey(withEntity.Entity)); if (detailEntity == null) { return; } DetailEntities.Add(detailEntity); SaveChangesAndNotify(new TDetailEntity[] { detailEntity }); }); }