sql-server - 自动插入记录到另一个表
问题描述
我有名为 AdditionalInsuranceLog、AdditionInsurance 和 Patient 的表
附加保险日志.cs
public partial class additionalInsuranceLog
{
public long PatientId { get; set; }
public byte AdditionInsuranceId { get; set; }
public DateTime ChangeDate { get; set; }
public virtual AdditionInsurance AdditionInsurance { get; set; }
public virtual Patient Patient { get; set; }
}
加法保险.cs
public partial class AdditionInsurance
{
public AdditionInsurance()
{
Patient = new HashSet<Patient>();
}
public byte Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Patient> Patient { get; set; }
}
病人.cs
public partial class Patient
{
public Patient()
{
}
public long Id { get; set; }
public string NationalCode { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string Education { get; set; }
public string CellPhone { get; set; }
public byte? AdditionInsurance { get; set; }
public DateTime EditDate { get; set; }
public AdditionInsurance AdditionInsuranceNavigation { get; set; }
}
AdditionInsurance 填写在 sql 中,我希望每次在 db 中插入或更新患者记录,如果 AdditionInsurance 字段发生更改,则会在附加保险日志表中插入一条新记录,日期为今天,我在我的多个地方插入或更新患者表web 应用程序,我想要一个最佳解决方案,而不是在我更新或插入患者记录的任何地方进行编码以在 additionalInsuranceLog 中插入记录我希望它始终完成,是否有最佳解决方案?
我正在使用 asp.net core 3 和 entityframework core
更新 我对触发器一无所知,我应该或可以将触发器用于我的目的吗?请告诉我有关如何做的详细信息?
解决方案
是的,您可以使用触发器。
像这样的东西。
CREATE TRIGGER AdditionInsurance_trg
ON AdditionInsurance
AFTER UPDATE,INSERT
AS
BEGIN
INSERT INTO AdditionalInsuranceLog(
PatientID,
Field1New,
Field1old)
SELECT
i.PatientID,
i.Field1New,
d.Field1Old
FROM inserted i
LEFT JOIN deleted d
ON i.item_id = d.item_id
END;
推荐阅读
- reactjs - 在 React 中设置响应式处理程序,然后在 scss 中使用它们
- python - 多行数据,想获取索引还是想办法按顺序获取
- mongodb - 如何在mongodb的另一个文档中过滤文档的字段?
- angular - angular 11 处理 http 响应
- macos - zsh 权限被拒绝
- angular - Angular:serve 命令需要在 Angular 项目中运行,但找不到项目定义
- linux - nbd-client 设置设备失败
- angular - writeValue 不称为 Angular 自定义表单控件
- python - 使用 Python 将 XML 转换为 CSV - 无法正确返回结果
- vulkan - 并发使用 VkSamplers?