Esempio n. 1
0
        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 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);
                }
            }
        }