在 AddBulkRequestAsync 方法中,插入新的 bulkPurchaseRequest 后,可以查询 BulkPurchaseRequests 表以获取总计数,然后使用 remainder 运算符 (“%”) 检查计数,然后根据结果将记录插入到 BulkPurchase 表中。
代码如下:
复制
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.
}
}
此外,您使用的是哪种数据库?是否支持 Trigger?如果它支持 SQL Server 数据库等触发器,则可以尝试在数据库中使用带有条件的触发器。
更新:
要使用触发器,
复制
--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,'test@gmail.com',0, GETDATE());
go
select * from BulkPurchaseRequests
go
select * from BulkPurchases
go
结果如下:在此屏幕截图中,我使用 sql query 语句插入数据,如果我从 Blazor 插入数据,它也会触发 SQL 触发器,并获得相同的结果。
有关触发器的更多详细信息,请参阅 CREATE TRIGGER (Transact-SQL)。
我不熟悉 SQL 端操作,如何创建触发器以插入我想要的方式?
可以通过 SSMS 打开 SQL 查询,然后使用 SQL 命令创建 SQL 触发器。请参阅我在回复中的更新。
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。