ASP.NET Core Web API - Entity Relation Problem

Cenk 1,041 Reputation points
2022-12-25T05:22:20.107+00:00

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:
273798-legacy-2022-12-25-081544.png

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

Here is the query:

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

Here are entities:

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; }  
    }  
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Cenk 1,041 Reputation points
    2022-12-27T13:20:43.343+00:00

    I added NuGet for lazy loading as follows:

    builder.Services.AddDbContext<OyunPalasDbContext>(  
        options => options.UseLazyLoadingProxies().UseSqlServer("name=ConnectionStrings:OyunPalasDbContext"));  
    

    But getting this error now:

    Cannot insert explicit value for identity column in table 'Coupons' when IDENTITY_INSERT is set to OFF.  
    

    it shouldn't be this hard to upgrade.


  2. Cenk 1,041 Reputation points
    2022-12-27T15:08:04.627+00:00

    I found the problem. When I lazy load, it gets the coupons which is what I want;

    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.

    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.

      _unitOfWork.GameConfirmResponseRepository.Insert(gameBankConfirmResponse[k]);  
    

    Can I remove coupons ID while mapping with Automapper?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.