Raysefo

Raysefo

  • 1.4k
  • 284
  • 149.5k

How to check if the record exists with the same value in the database

Jun 5 2023 1:01 PM

Hello there,
In my Blazor Server application, I am inserting a list of data into a database as follows with EF Core 6;

public async Task AddStockAsync(List<GameBank> gameBank)
{
   await _oyunPalasContext.GameBanks.AddRangeAsync(gameBank);
   await _oyunPalasContext.SaveChangesAsync();
}

I wonder if I can check the serial and pin inserted before? I am using an SQL server and EF Core 6.

public partial class GameBank
{
    public int GameBankId { get; set; }
    public Guid ReferenceId { get; set; }
    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; }
    public int Used { get; set; }
    public string? ProductDescription { get; set; }
    public string? Currency { get; set; }
    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; }
    public int Status { get; set; }
    public virtual GameBankPin coupons { get; set; }
    public string? ClientTrxRef { get; set; }
}
public partial class GameBankPin
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    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; }
}

There are tens of thousands of data in the database and the list to be compared (List<GameBank> gameBank) might be 5000 records. Is there a more performant method instead of checking one by one in the loop?


Answers (2)