public override async Task <Basket> Query(CreateOrGetUnconfirmedBasketCommand command) { var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); if (user == null) { return(null); } var basket = await DbContext.Baskets .Include(o => o.BasketItems) .Where(o => o.UserId == user.Id && !o.IsConfirmed) .FirstOrDefaultAsync(); if (basket == null) { basket = new Basket() { UserId = user.Id }; DbContext.AddOrUpdate(basket); await DbContext.SaveChangesAsync(); } else if (basket.BasketItems.Any(o => !o.ProductId.HasValue)) { foreach (var basketItem in basket.BasketItems.Where(o => !o.ProductId.HasValue)) { DbContext.ActionByEntityState(basketItem, EntityState.Deleted); } await DbContext.SaveChangesAsync(); } return(basket); }
private void HandeItemDeleted(IIdenitifiable item, Type dbType) { var obj = Mapper.Map(item, item.GetType(), dbType) as ITable; obj.IsDeleted = true; DbContext.AddOrUpdate(obj); }
public override async Task <Product> Query(SaveProductQueryCommand command) { DbContext.AddOrUpdate(command.Product); await DbContext.SaveChangesAsync(); _cache.ClearRegion(CacheRegions.Product); return(command.Product); }
/// <summary> /// Adds or updates (if exists) the entities. /// </summary> /// <typeparam name="TEntity">The type of <paramref name="entities"/>.</typeparam> /// <param name="dbContext">The <see cref="DbContext"/>.</param> /// <param name="entities">The <see cref="object"/>'s of type <typeparamref name="TEntity"/>.</param> /// <returns>A <see cref="EntityEntry{TEntity}"/>.</returns> public static void AddOrUpdateMany <TEntity>(this DbContext dbContext, IEnumerable <TEntity> entities) where TEntity : class { if (entities == null) { throw new ArgumentNullException(nameof(entities)); } foreach (var entity in entities) { dbContext.AddOrUpdate(entity); } }
/// <summary> /// Add or update entity. If entity already exists in the source /// set and values has not been altered set the state to Unchanged, /// else if values has been changed set the state of changed properties /// to Modified, otherwise set the state of entity to Added. Do it for /// all child entities also. /// </summary> /// <exception cref="ArgumentNullException"> /// When context or entity is null. /// </exception> /// <typeparam name="TEntity">Type of entity.</typeparam> /// <param name="context">Context to work on.</param> /// <param name="entity">Entity to add or update.</param> /// <returns>IManualGraphManager associated with current context to work on further.</returns> public static IManualGraphManager <TEntity> AddOrUpdate <TEntity>( this DbContext context, TEntity entity) where TEntity : class { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (entity == null) { throw new ArgumentNullException(nameof(entity)); } return(context.AddOrUpdate(entity, true)); }
public override async Task <Basket> Query(AddProductToBasketCommand command) { var product = await _getProductQuery.Query(new GetProductCommand(command.ProductId, _workContext.WorkingLanguageId)); if (product == null) { return(null); } var basket = await _createOrGetUnconfirmedBasketQuery.Query(new CreateOrGetUnconfirmedBasketCommand()); if (basket == null) { return(null); } var basketItem = basket.BasketItems.FirstOrDefault(o => o.ProductId == command.ProductId && o.ProductDetailId == command.ProductDetailId); if (basketItem == null) { basketItem = new BasketItem(); basketItem.BasketId = basket.Id; basketItem.ProductDetailId = command.ProductDetailId; basketItem.ProductId = product.Id; basketItem.Price = product.GetTotalPrice(); basketItem.Quantity = command.Quantity; } else { basketItem.Quantity += command.Quantity; } DbContext.AddOrUpdate(basketItem); await DbContext.SaveChangesAsync(); return(basket); }
private void HandleItemAddedOrUpdated(IIdenitifiable item, Type dbType) { DbContext.AddOrUpdate(Mapper.Map(item, item.GetType(), dbType) as ITable); }
public override void PredefineData(DbContext dbContext) { dbContext.AddOrUpdate(All <VoucherState>()); }
public void AddOrUpdate(T entity) { //uses DbContextExtensions to check value of primary key _context.AddOrUpdate(entity); }
public void Salvar(TEntity entidade) { _dbContext.AddOrUpdate(entidade); _dbContext.SaveChanges(); }
public virtual void AddOrUpdate(TEntity entity) { //context.Set<TEntity>().AddOrUpdate(entity); context.AddOrUpdate(entity); }