通过存储过程在数据库中执行更改时不调用触发器

时间:2019-06-29 09:21:54

标签: asp.net-mvc-4 stored-procedures entity-framework-6 database-trigger sql-server-2014-express

我有以下问题:当我在SQL Server Management Studio中插入记录时,我的触发器工作良好。但是,当我通过存储过程从网站插入记录时,触发器不会触发。

这是我的控制器代码

[HttpPost]
public ActionResult JobWorkOutNew(PRCJobWorkOut p)
{
        if (p.SCODE != null)
        {
            var spres = Db.Sp_PRCJobworkOut_Insert(p.JobChallan, Convert.ToDateTime(DateTime.Today.ToShortDateString()), p.SCODE, p.ExciseChallan, p.JOBWorkID, p.Weight, p.ApproxValue, p.Packages, p.Transporter, p.LRNO, p.LRDT, p.VehicleNo, p.DispatchTo, p.Freight, p.Remarks, p.GoodType, p.IGST, p.SGST, p.CGST, p.Unit);

            if (spres == 1 || spres == -1)
            {
                if (p.SubItems.Count() > 0)
                {
                    var subitm = p.SubItems.ToList();

                    for (var i = 0; i < p.SubItems.Count(); i++)
                    {
                        var sb = subitm[i];
                        var spressub = Db.Sp_PRCJobworkOutSub_Insert(p.JobChallan, i + 1, sb.PRCNO, sb.HEATNO, sb.QTY, sb.Weight, sb.Status);

                        if (spressub == 1 || spressub == -1)
                        {
                            continue;
                        }
                        else
                        {
                            this.AddToastMessage("Error", "Something went wrong in insert with sub PRC Jobwork", ToastType.Error);
                            break;
                        }
                    }

                    var global = Db.GlobalSettings.FirstOrDefault();
                    global.JOBChallan = (Convert.ToInt32(global.JOBChallan) + 1).ToString();

                    Db.SaveChanges();

                    return Json(spres, JsonRequestBehavior.AllowGet);
                }
            }
        }

        return View();
}

这是我在数据库PrcJobworkOutSub表上的触发器:

ALTER TRIGGER [dbo].[PRCJOBChallanStatus] 
ON [dbo].[PRCJobWorkOutSub] 
AFTER INSERT, UPDATE, DELETE
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE P 
    SET P.status = (SELECT MIN(Status) 
                    FROM PRCJOBWorkOUTSub 
                    WHERE PRCJOBWorkOUTSub.JobChallan = P.Jobchallan) 
    FROM PRCJOBWorkOUT P 
    FULL OUTER JOIN PRCJOBWorkOUTSub S on P.JobChallan = S.JobChallan
END

0 个答案:

没有答案