LINQ未引起插入触发器

本文关键字:插入 触发器 LINQ | 更新日期: 2023-09-27 18:10:13

我又问了一个LINQ问题。

我们的数据库设置为当SQL插入发生时有一个事件触发器。

现在,当我使用LINQ时,这个触发器不会被调用。我是否需要做一些特别的事情来使数据库看到LINQ命令作为插入?(下面的代码)。我应该说数据正在正确地输入到数据库中,但是触发器没有发生。

提前感谢。

LINQ代码:

private void SaveToWebsure()
{
    using (MagTestDataContext context = new MagTestDataContext())
    {
        //create new instance of tblPolicy object
        tblPolicy policy = new tblPolicy();
        //generate PolicyID number
        policyNo = context.ExecuteQuery<int>("DECLARE @ret INT; EXEC spNextPolicyID @ret OUTPUT; SELECT @ret").Single();
        //add values to field
        policy.PolicyID = policyNo;
        policy.RecordType = "New Business";
        policy.SchemeID = 17;
        policy.Status = "Quote";
        policy.Title = ddTitle.Text + ' ' + tbSurname.Text;
        policy.PolicyHolder = ddTitle.Text + ' ' + tbFirstName.Text + ' ' + tbSurname.Text;
        policy.SearchKey = tbSurname.Text;
        policy.EMail = tbEmail.Text;
        policy.Telephone = tbTelephone.Text;
        policy.Address1 = tbAddressLine1.Text;
        policy.Address2 = tbAddressLine2.Text;
        policy.Address3 = tbAddressLine3.Text;
        policy.Address4 = tbAddressLine4.Text;
        policy.PostCode = tbPostcode.Text;
        policy.rowguid = System.Guid.NewGuid();
        policy.Comments = "Current/Previous Insurer: " + tbInsurer.Text + "; " + "Reason for refused insurance: " + ddReason.SelectedItem.Text + "; " + "Further reasons specified: " + tbOther.Text;
        //insert new contact_detail object
        context.tblPolicies.InsertOnSubmit(policy);
        //submit changes to database
        context.SubmitChanges();
    }
存储过程:

 ALTER PROCEDURE spNextPolicyID @PolicyID int output AS
    begin tran
      /* update tblIdNumbers set ApplicantId = ApplicantId
      set @Policyid = (select ApplicantID from tblIdNumbers)
      update tblIdNumbers set ApplicantId = ApplicantId + 1 */
    set Rowcount 0
    SET NOCOUNT ON
    exec dbo.spNextIdNumber @PolicyID Output,'PolicyId'
    commit tran

LINQ未引起插入触发器

LINQ在后台没有做任何特别的事情。您应该检查

的几件事
  • 使用SQL Profiler查看正在生成的查询和对DB运行的查询
  • 检查是否在查询运行前启用了触发器
  • 提示:如果你要移动大量数据,尽可能避免触发器(每种情况都不同,只是说一下),我们通常在处理大数据时从代码中禁用/启用触发器