/// <summary>
        /// Create record of relationship between entities in database
        /// </summary>
        protected async Task <TEntityRel> Create <TReference1, TReference2>(TEntityRel item) where TReference1 : class, IEntity
            where TReference2 : class, IEntity
        {
            var(result, idRef1Value, idRef2Value) = GetRecordAndIdsForRelationship(item);

            if (!ExistsInDb <TReference1>(idRef1Value))
            {
                throw new ObjectNotFoundException(typeof(TReference1).FullName, idRef1Value);
            }
            else if (!ExistsInDb <TReference2>(idRef2Value))
            {
                throw new ObjectNotFoundException(typeof(TReference2).FullName, idRef2Value);
            }

            if (result != null)
            {
                throw new InvalidObjectRelationshipException("This reference already exists", typeof(TEntityRel).FullName, idRef1Value, idRef2Value);
            }

            await _context.Set <TEntityRel>().AddAsync(item);

            await _context.SaveChangesAsync();

            return(item);
        }
예제 #2
0
        /// <summary>
        /// Saves the Person.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <int> SaveAsync(PersonDto item, CancellationToken cancellationToken = default)
        {
            try
            {
                var dbSet      = _dbContext.Persons;
                var itemFromDb = dbSet.Find(item.Id);
                if (itemFromDb != null)
                {
                    itemFromDb.Biography      = item.Biography;
                    itemFromDb.BirthDate      = item.BirthDate;
                    itemFromDb.DeathDate      = item.DeathDate;
                    itemFromDb.FirstName      = item.FirstName;
                    itemFromDb.GenderId       = item.GenderId;
                    itemFromDb.LastName       = item.LastName;
                    itemFromDb.PatronymicName = item.PatronymicName;

                    dbSet.Update(itemFromDb);
                }
                else
                {
                    itemFromDb = new Person
                    {
                        Biography      = item.Biography,
                        BirthDate      = item.BirthDate,
                        DeathDate      = item.DeathDate,
                        FirstName      = item.FirstName,
                        GenderId       = item.GenderId,
                        LastName       = item.LastName,
                        PatronymicName = item.PatronymicName
                    };

                    dbSet.Add(itemFromDb);
                }

                await _dbContext.SaveChangesAsync(cancellationToken);

                return(item.Id);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
            }

            return(-1);
        }