ASP.NET Core Web API - Entity Relation Problem

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
361
Programming Experience
10+
Hi,

I am upgrading my legacy ASP.NET web API to Core. This upgrading process is harder than I expected. The problem I'm having now is, this query gets the GameBank and GameBankPin in my legacy application. But the same code doesn't work in the Core application, unfortunately. GameBankPin is NULL.

Legacy:
Legacy 2022-12-25 081544.png


Core 6:
Core 2022-12-25 081823.png


Query:
C#:
     var gameBankResult =
       //Query GameBank database
       _unitOfWork.GameBankRepository.GetGames(g =>
       g.productCode == requestDto.productCode && g.referenceId == Guid.Empty);

Entities:
C#:
public class GameBank
     {
         public int GameBankID { get; set; }
         public Guid referenceId { get; set; }
         [DisplayName("Product Code")]
         public string productCode { get; set; }
         public int quantity { get; set; }
         public string? version { get; set; }
         public DateTime? requestDateTime { get; set; } = DateTime.Now;
         public int? customerID { get; set; }
         public string? password { get; set; }
         public DateTime? responseDateTime { get; set; } = DateTime.Now;
         public string? initiationResultCode { get; set; }
         public string? companyToken { get; set; }
         [DisplayName("Reconciled")]
         public int used { get; set; }
         [DisplayName("Description")]
         public string? productDescription { get; set; }
         public string? currency { get; set; }
         [DisplayName("Unit Price")]
         public double unitPrice { get; set; }
         public double totalPrice { get; set; }
         public string? ApplicationCode { get; set; }
         public double estimateUnitPrice { get; set; }
         public string? validatedToken { get; set; }
         public string? signature { get; set; }
         [DisplayName("Confirm/Cancel Status")]
         public int status { get; set; }
         public virtual List<GameBankPin> coupons { get; set; }
         public string? clientTrxRef { get; set; }
     }
 public class GameBankPin
     {
         public int Id { get; set; }
         public int GameBankID { get; set; }
         public DateTime? expiryDate { get; set; }
         public string Serial { get; set; }
         public string Pin { get; set; }
         public virtual GameBank GameBanks { get; set; }
     }
 
I found the problem. When I lazy load, it gets the coupons which are what I want;
C#:
     var gameBankResult = 
                     //Query GameBank database
                     _unitOfWork.GameBankRepository.GetGames(g =>
                         g.productCode == requestDto.productCode && g.referenceId == Guid.Empty).Take(100).ToList();


But automapper maps the coupons to GameConfirmResponse where I am trying to insert them.
C#:
var gameBankConfirmResponse =
                     _mapper.Map<IList<GameBank>, IList<GameConfirmResponse>>(gameBankResult);



Where it gets the Cannot insert explicit value for identity column in table 'Coupons' when IDENTITY_INSERT is set to OFF
C#:
_unitOfWork.GameConfirmResponseRepository.Insert(gameBankConfirmResponse[k]);


Can I remove coupons while mapping with Automapper?
 
I'm not use I'd use lazy loading with a mapper..

..you can do it like I showed, explicitly loading the data you want
 
Back
Top Bottom