I have a solution which has class libraries (business entities, business service, data models) and a web api project. I am using generic 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?
Keep your friends close and your enemies even closer
Member
527 Points
2729 Posts
Unit of Work usage question
Jul 01, 2019 07:29 AM|cenk1536|LINK
Hello,
I have a solution which has class libraries (business entities, business service, data models) and a web api project. I am using generic repository and unit of work. I am trying to implement my logic inside of business service as follows;
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?
All-Star
48530 Points
18075 Posts
Re: Unit of Work usage question
Jul 01, 2019 07:41 AM|PatriceSc|LINK
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 ?
Member
527 Points
2729 Posts
Re: Unit of Work usage question
Jul 01, 2019 07:47 AM|cenk1536|LINK
yes PatriceSc, I am using code first approach.
Here is my insert in generic repository.
I don't get it after every add should I use;
I think there was a change in the table fields :)