Hi @Cenk
In the AddBulkRequestAsync method, after insert the new bulkPurchaseRequest, you can query the BulkPurchaseRequests table to get the total count, then use the remainder operator("%") to check the count and then based on the result to insert records into the BulkPurchase table.
Code like this:
public async Task AddBulkRequestAsync(BulkPurchaseRequest bulkPurchaseRequest)
{
await _oyunPalasContext.BulkPurchaseRequests.AddAsync(bulkPurchaseRequest);
await _oyunPalasContext.SaveChangesAsync();
//get the total count of the BulkPurchaseRequests
int totalcount = _oyunPalasContext.BulkPurchaseRequests.Count();
if(totoalcount % 100 == 0){
//insert record into the BulkPurchase table.
}
}
Besides, which kind of database you are using? Whether it support Trigger? If it supports trigger such as SQL Server Database, you can try to use trigger with condition in the database.
Update:
To use trigger,
--create the trigger
Create Trigger [dbo].[bprinserttrigger]
on [dbo].[BulkPurchaseRequests]
after insert
AS
Begin
set nocount on;
Declare @totalcount int
select @totalcount = COUNT(*) from BulkPurchaseRequests
if @totalcount % 10 = 0
begin
//get the first record from the BulkPurchaseRequests, and then insert it into the BulkPurchases table. You can change it based on your requirement, pay more attention to the foreign key BulkPurchaseRequestId.
INSERT INTO BulkPurchases(BulkID, Amount, ProductCode,[Status], PurchaseDateTime, BulkPurchaseRequestId)
SELECT top 1 Id as id1, TotalAmount, ProductCode, [Status],RequestDateTime, Id
FROM BulkPurchaseRequests
order by id;
end
end
go
--insert data into the BulkPurchaseRequests table
INSERT INTO BulkPurchaseRequests (TotalAmount, ProductCode, [Description], Email,[Status], RequestDateTime)
VALUES (1000, 0187209, 0187209,'******@gmail.com',0, GETDATE());
go
select * from BulkPurchaseRequests
go
select * from BulkPurchases
go
The result as below: in this screenshot, I insert the data using the sql query statement, if I insert data from Blazor, it will also trigger the SQL trigger, and get the same result.
More detail information about trigger, see CREATE TRIGGER (Transact-SQL).
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.
Best regards,
Dillion