Example #1
0
        public override async Task UpdateAsync(int id, int?parentId, PersonEntity entity, IRelationContainer?relations)
        {
            var dbEntity = await _dbContext.Persons.Include(x => x.Countries).FirstOrDefaultAsync(x => x._Id == id);

            dbEntity.Name = entity.Name;

            var newCountries = relations?.GetRelatedElementIdsFor <CountryEntity, int>();

            if (newCountries != null)
            {
                foreach (var country in dbEntity.Countries.Where(x => !newCountries.Contains(x.CountryId.Value)).ToList())
                {
                    dbEntity.Countries.Remove(country);
                }
                foreach (var countryId in newCountries.Where(id => !dbEntity.Countries.Select(x => x.CountryId.Value).Contains(id)).ToList())
                {
                    dbEntity.Countries.Add(new PersonCountryEntity {
                        CountryId = countryId
                    });
                }
            }

            _dbContext.Persons.Update(dbEntity);
            await _dbContext.SaveChangesAsync();
        }
Example #2
0
        public override async Task <PersonEntity> InsertAsync(int?parentId, PersonEntity entity, IRelationContainer?relations)
        {
            entity.Countries = relations?.GetRelatedElementIdsFor <CountryEntity, int>()?.Select(id => new PersonCountryEntity {
                CountryId = id
            }).ToList() ?? new List <PersonCountryEntity>();

            var entry = _dbContext.Persons.Add(entity);
            await _dbContext.SaveChangesAsync();

            return(entry.Entity);
        }