1

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

1 回答 1

0

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

  • 使用 SQL Profiler 查看正在生成的查询以及针对 DB 运行的查询
  • 检查并查看触发器是否在查询运行之前启用
  • 提示:如果您要移动大量数据,请尽可能避免触发器(每种情况都不同,只是说)我们通常在处理大数据时禁用/启用代码中的触发器
于 2013-08-06T17:20:22.843 回答