locked
Unit of Work usage question RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I have a solution which has class libraries (business entities, business service, data models) and a web <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">api</g> project. I am using <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="11" data-gr-id="11">generic</g> repository and unit of work. I am trying to implement my logic inside of business service as follows;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Transactions;
    using AutoMapper;
    using BusinessEntities;
    using BusinessService.Utility;
    using DataModels;
    using Newtonsoft.Json;
    
    namespace BusinessService
    {
        public class GameServices : IGameServices
        {
            private readonly UnitOfWork _unitOfWork;
    
            /// <summary>
            /// Public constructor.
            /// </summary>
            public GameServices(UnitOfWork unitOfWork)
            {
                _unitOfWork = unitOfWork;
            }
            
            /// <summary>
            /// Creates a product
            /// </summary>
            /// <param name="requestDto"></param>
            /// <returns></returns>
            public async Task<GameConfirmResponse> GamePurchase(RequestDto requestDto)
            {
                using (var scope = new TransactionScope())
                {
    
                    //Transform DTO into GameRequest for calling Razer Initiate
                    var config = new MapperConfiguration(cfg => {
                        cfg.CreateMap<RequestDto, GameRequest>();
                    });
                    var iMapper = config.CreateMapper();
                    var gameRequest = iMapper.Map<RequestDto, GameRequest>(requestDto);
                   
                    gameRequest = Utilities.CreateSignature(gameRequest, RequestType.Initiate);
                    //Unique reference ID
                    gameRequest.referenceId = Guid.NewGuid().ToString();
                    try
                    {
                        //Add request into database
                        _unitOfWork.GameRepository.Insert(gameRequest);
                    }
                    catch (Exception e)
                    {
                        
                        throw e;
                    }
                    
    
                    #region Call Razer initiate
    
                    var response = await Utilities.CallRazer(gameRequest, "purchaseinitiation");
                    //Read response
                    var htmlResponse = await response.Content.ReadAsStringAsync();
    
                    var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);
                    //Adding response into database
                    _unitOfWork.GameResponseRepository.Insert(gameResponse);
                    
                    if (gameResponse.initiationResultCode == "00")
                    {
    
                    }
                    #endregion
                    await _unitOfWork.SaveAsync();
                    
                    var gameConfirmResponse = Mapper.Map<RequestDto, GameConfirmResponse>(requestDto);
                    scope.Complete();
                    
                    return gameConfirmResponse;
                }
            }
    
            
            public enum RequestType
            {
                Initiate = 0,
                Confirm = 1
                
            }
    
        }
    }
    

    I am getting this error; System.InvalidOperationException: The model backing the 'GameContext' context has changed since the database was created. Consider using Code First Migrations to update the database. My plan was to add changes to the database and at the end save all the changes once for all. What is the proper way to do it?

    Monday, July 1, 2019 7:29 AM

Answers

All replies

  • User753101303 posted

    Hi,

    It is unrelated to updating data. It means you changed the GameContext without applying changes to the underlying database. See https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/

    You are using code first to define your db structure ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 1, 2019 7:41 AM
  • User-1104215994 posted

    yes PatriceSc, I am using code first approach.

    Here is my insert in <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="7" data-gr-id="7">generic</g> repository.

    public virtual void Insert(TEntity entity)
            {
                dbSet.Add(entity);
            }

    I don't get it after every add should I use;

    await _unitOfWork.SaveAsync();

    I think there was a change in the table fields :)

    Monday, July 1, 2019 7:47 AM