Hi,@Cenk. Welcome Microsoft Q&A.
For improved performance when checking whether records already exist in the database before inserting, you could leverage the capabilities of Entity Framework Core (EF Core) and perform a batch insert operation with a single database query.
You can try to refer to the following codes according to your needs.
Extract the Serial and Pin values from the gameBank list:
var serials = gameBank.Select(g => g.coupons.GameBankPin);
var pins = gameBank.Select(g => g.coupons.Pin);
Query the database to check if any records already exist with the same Serial and Pin values:
var existingRecords = await _oyunPalasContext.GameBanks
.Where(g => serials.Contains(g.coupons.Serial) && pins.Contains(g.coupons.Pin))
.ToListAsync();
Identify the Serial and Pin values that already exist in the database:
var existingSerials = existingRecords.Select(g => g.coupons.Serial);
var existingPins = existingRecords.Select(g => g.coupons.Pin);
Filter the gameBank list to exclude the records that already exist in the database:
var newGameBank = gameBank.Where(g => !existingSerials.Contains(g.coupons.Serial) || !existingPins.Contains(g.coupons.Pin)).ToList();
Perform the batch insert operation with the filtered newGameBank list:
await _oyunPalasContext.GameBanks.AddRangeAsync(newGameBank);
await _oyunPalasContext.SaveChangesAsync();
By performing the batch insert operation, you reduce the number of database queries needed to check for existing records, resulting in improved performance compared to checking one record at a time within a loop.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.