public void SynchronizeNavigationProperties(Order order) { Northwind.Order northwindOrder = _northwindDbContext.Orders.First(x => x.OrderID == order.NorthwindId); order.OrderItems = new List <OrderItem>(); foreach (Order_Detail orderDetail in northwindOrder.Order_Details) { OrderItem orderItem = _gameStoreDbContext.Set <OrderItem>() .FirstOrDefault(oi => oi.NorthwindOrderId == orderDetail.OrderID && oi.NorthwindProductId == orderDetail.ProductID); if (orderItem != null) { order.OrderItems.Add(orderItem); } } if (_gameStoreDbContext.Entry(order).State == EntityState.Detached) { _gameStoreDbContext.Set <Order>().Attach(order); } _gameStoreDbContext.Entry(order).State = EntityState.Modified; }
private void SynchronizeNavigationProperties() { foreach (Game game in _gameStoreDbContext.Set <Game>().Where(x => x.NorthwindId != null).ToList()) { _gameNavigationPropertiesSynchronizer.SynchronizeNavigationProperties(game); } foreach (Publisher publisher in _gameStoreDbContext.Set <Publisher>().Where(x => x.NorthwindId != null).ToList()) { _publisherNavigationPropertiesSynchronizer.SynchronizeNavigationProperties(publisher); } foreach (Genre genre in _gameStoreDbContext.Set <Genre>().Where(x => x.NorthwindId != null).ToList()) { _genreNavigationPropertiesSynchronizer.SynchronizeNavigationProperties(genre); } foreach (Order order in _gameStoreDbContext.Set <Order>().Where(x => x.NorthwindId != null).ToList()) { _orderNavigationPropertiesSynchronizer.SynchronizeNavigationProperties(order); } foreach (OrderItem orderItem in _gameStoreDbContext.Set <OrderItem>() .Where(x => x.NorthwindOrderId != null && x.NorthwindProductId != null).ToList()) { _orderItemNavigationPropertiesSynchronizer.SynchronizeNavigationProperties(orderItem); } _gameStoreDbContext.SaveChanges(); }
public void SynchronizeNavigationProperties(Game game) { Product product = _northwindDbContext.Products.First(x => x.ProductID == game.NorthwindId); game.Genres = new List <Genre>(); foreach (Genre genre in _gameStoreDbContext.Set <Genre>() .Where(x => x.NorthwindId == product.Category.CategoryID) .ToList()) { game.Genres.Add(genre); } game.Publisher = _gameStoreDbContext.Set <Publisher>() .FirstOrDefault(x => x.NorthwindId == product.Supplier.SupplierID); if (game.Publisher != null) { game.PublisherId = game.Publisher.PublisherId; } game.BasketItems = new List <BasketItem>(); foreach (BasketItem basketItem in _gameStoreDbContext.Set <BasketItem>() .Where(bi => bi.GameId == game.GameId) .ToList()) { game.BasketItems.Add(basketItem); } game.Comments = new List <Comment>(); foreach (Comment comment in _gameStoreDbContext.Set <Comment>() .Where(c => c.GameId == game.GameId) .ToList()) { game.Comments.Add(comment); } if (_gameStoreDbContext.Entry(game).State == EntityState.Detached) { _gameStoreDbContext.Set <Game>().Attach(game); } _gameStoreDbContext.Entry(game).State = EntityState.Modified; }
public void SynchronizeNavigationProperties(OrderItem orderItem) { orderItem.Order = _gameStoreDbContext.Set <Order>() .First(o => o.NorthwindId == orderItem.NorthwindOrderId); orderItem.OrderId = orderItem.Order.OrderId; orderItem.Game = _gameStoreDbContext.Set <Game>() .First(g => g.NorthwindId == orderItem.NorthwindProductId); orderItem.GameId = orderItem.Game.GameId; if (_gameStoreDbContext.Entry(orderItem).State == EntityState.Detached) { _gameStoreDbContext.Set <OrderItem>().Attach(orderItem); } _gameStoreDbContext.Entry(orderItem).State = EntityState.Modified; }
public void DeleteUnusedEntities() { DbSet <TEntity> gameStoreDbSet = _gameStoreDbContext.Set <TEntity>(); DbSet <TNorthwindEntity> northwindDbSet = _northwindDbContext.Set <TNorthwindEntity>(); foreach (TEntity entity in gameStoreDbSet.Where(x => x.NorthwindId != null)) { if (northwindDbSet.Find(entity.NorthwindId) == null) { gameStoreDbSet.Remove(entity); } } }
public void SynchronizeNavigationProperties(Genre genre) { Category category = _northwindDbContext.Categories.First(x => x.CategoryID == genre.NorthwindId); genre.Games = new List <Game>(); foreach (Product product in category.Products) { Game game = _gameStoreDbContext.Set <Game>() .FirstOrDefault(g => g.NorthwindId == product.ProductID); if (game != null) { genre.Games.Add(game); } } if (_gameStoreDbContext.Entry(genre).State == EntityState.Detached) { _gameStoreDbContext.Set <Genre>().Attach(genre); } _gameStoreDbContext.Entry(genre).State = EntityState.Modified; }
public void SynchronizeNavigationProperties(Publisher publisher) { Supplier supplier = _northwindDbContext.Suppliers.First(x => x.SupplierID == publisher.NorthwindId); publisher.Games = new List <Game>(); foreach (Product product in supplier.Products) { Game game = _gameStoreDbContext.Set <Game>() .FirstOrDefault(g => g.NorthwindId == product.ProductID); if (game != null) { publisher.Games.Add(game); } } if (_gameStoreDbContext.Entry(publisher).State == EntityState.Detached) { _gameStoreDbContext.Set <Publisher>().Attach(publisher); } _gameStoreDbContext.Entry(publisher).State = EntityState.Modified; }
public void Synchronize(Order_Detail orderDetail) { DbSet <OrderItem> gameStoreDbSet = _gameStoreDbContext.Set <OrderItem>(); DbSet <Order_Detail> northwindDbSet = _northwindDbContext.Set <Order_Detail>(); if (orderDetail.IsProxy()) { orderDetail = orderDetail.UnProxy(_northwindDbContext); } var northwindOrderItem = Mapper.Map <OrderItem>(orderDetail); OrderItem gameStoreOrderItem = gameStoreDbSet .FirstOrDefault(x => x.NorthwindOrderId == orderDetail.OrderID && x.NorthwindProductId == orderDetail.ProductID); if (gameStoreOrderItem == null) { _gameStoreDbContext.Set <OrderItem>().Add(northwindOrderItem); } else { if (gameStoreOrderItem.IsProxy()) { gameStoreOrderItem = gameStoreOrderItem.UnProxy(_gameStoreDbContext); } northwindOrderItem.OrderItemId = gameStoreOrderItem.OrderItemId; ICustomComparer comparer = _comparerFactory.GetComparer(typeof(OrderItem)); if (!comparer.AreEqual(gameStoreOrderItem, northwindOrderItem)) { _gameStoreDbContext.Entry(gameStoreOrderItem).CurrentValues.SetValues(northwindOrderItem); } } }
public void DeleteUnusedEntities() { DbSet <OrderItem> gameStoreDbSet = _gameStoreDbContext.Set <OrderItem>(); DbSet <Order_Detail> northwindDbSet = _northwindDbContext.Set <Order_Detail>(); foreach ( OrderItem orderItem in gameStoreDbSet.Where(x => x.NorthwindOrderId != null && x.NorthwindProductId != null)) { if (!northwindDbSet.Any(od => od.OrderID == orderItem.NorthwindOrderId && od.ProductID == orderItem.NorthwindOrderId)) { gameStoreDbSet.Remove(orderItem); } } }
public void Synchronize(TNorthwindEntity northwindEntity) { DbSet <TEntity> tEntityDbSet = _gameStoreDbContext.Set <TEntity>(); if (northwindEntity.IsProxy()) { northwindEntity = northwindEntity.UnProxy(_northwindDbContext); } var entityFromNorthwind = Mapper.Map <TEntity>(northwindEntity); string northwindIdPropertyName = typeof(TNorthwindEntity).IdentifierPropertyName(); var northwindEntityId = (int)northwindEntity.GetPropValue(northwindIdPropertyName); TEntity gameStoreEntity = tEntityDbSet .FirstOrDefault(x => x.NorthwindId == northwindEntityId); if (gameStoreEntity == null) { tEntityDbSet.Add(entityFromNorthwind); } else { if (gameStoreEntity.IsProxy()) { gameStoreEntity = gameStoreEntity.UnProxy(_gameStoreDbContext); } string gameStoreIdPropertyName = typeof(TEntity).IdentifierPropertyName(); PropertyInfo idPropertyInfo = typeof(TEntity).GetProperty(gameStoreIdPropertyName); var gameStoreEntityId = (int)gameStoreEntity.GetPropValue(gameStoreIdPropertyName); idPropertyInfo.SetValue(entityFromNorthwind, gameStoreEntityId, null); ICustomComparer comparer = _comparerFactory.GetComparer(typeof(TEntity)); if (!comparer.AreEqual(gameStoreEntity, entityFromNorthwind)) { _gameStoreDbContext.Entry(gameStoreEntity).CurrentValues.SetValues(entityFromNorthwind); } } }
public async Task <TEntity> AddAsync(TEntity entity) { await context.Set <TEntity>().AddAsync(entity); return(entity); }
public GenericRepository(GameStoreDbContext context) { _context = context; _dbSet = context.Set <TEntity>(); }