public SaveResult InsertOrUpdate(ref InventoryDTO inventory) { try { using (var context = DataAccessHelper.CreateContext()) { long characterId = inventory.CharacterId; short slot = inventory.Slot; short type = inventory.Type; Inventory entity = context.inventory.SingleOrDefault(c => c.Type.Equals(type) && c.Slot.Equals(slot) && c.CharacterId.Equals(characterId)); if (entity == null) //new entity { inventory = Insert(inventory, context); return SaveResult.Inserted; } else //existing entity { inventory.InventoryId = entity.InventoryId; inventory = Update(entity, inventory, context); return SaveResult.Updated; } } } catch (Exception e) { Logger.Log.ErrorFormat(Language.Instance.GetMessageFromKey("UPDATE_ACCOUNT_ERROR"), inventory.InventoryId, e.Message); return SaveResult.Error; } }
private InventoryDTO Insert(InventoryDTO inventory, OpenNosContainer context) { Inventory entity = new Inventory() { CharacterId = inventory.CharacterId, InventoryItemId = inventory.InventoryItemId, Slot = inventory.Slot, Type = inventory.Type, }; context.inventory.Add(entity); context.SaveChanges(); return Mapper.Map<InventoryDTO>(entity); }
private InventoryDTO Update(Inventory entity, InventoryDTO inventory, OpenNosContainer context) { using (context) { var result = context.inventory.SingleOrDefault(c => c.InventoryId == inventory.InventoryId); if (result != null) { result = Mapper.Map<InventoryDTO, Inventory>(inventory, entity); context.SaveChanges(); } } return Mapper.Map<InventoryDTO>(entity); }
private InventoryDTO Insert(InventoryDTO inventory, OpenNosContainer context) { Inventory entity = Mapper.Map<Inventory>(inventory); context.inventory.Add(entity); context.SaveChanges(); return Mapper.Map<InventoryDTO>(entity); }