LINQ不会导致插入触发器

时间:2013-04-19 14:56:11

标签: c# asp.net sql database linq

我再次提出另一个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

1 个答案:

答案 0 :(得分:0)

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

  • 使用SQL事件探查器查看正在生成的查询以及针对数据库运行的内容
  • 检查并查看在查询运行之前是否启用了触发器
  • 提示:如果您正在移动大量数据,请尽可能避免触发(每种情况都不同,只是说)我们通常在处理大数据时禁用/启用代码触发器