插入使用实体框架后,为什么(SQL severtrigger)没有触发? [英] Why (SQL severtrigger) does not fired after insert using entity framework?

查看:98
本文介绍了插入使用实体框架后,为什么(SQL severtrigger)没有触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have an c# application using Entity Framework and i create a trigger in the database fired after inserting , updating.

I notice that the trigger does not fired when i insert or update using EF code, on the other side the trigger fired when i insert or update through sql server





我尝试过:





What I have tried:

USE [HrProject]
GO
/****** Object:  Trigger [dbo].[updateLoanFinishing]    Script Date: 18/12/2016 8:24:49 ص ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER	TRIGGER	 [dbo].[updateLoanFinishing] ON [dbo].[Installments_Paids] AFTER INSERT , UPDATE , DELETE

AS
BEGIN
DECLARE	@totalLoan decimal , @totalpaid decimal , @balance decimal	

SELECT @totalLoan =	l.Total_Amount FROM dbo.Loans l

SELECT	@totalpaid = ISNULL(SUM(ip.Paid_Amount) ,0)  FROM dbo.Installments_Paids ip 
 
SELECT @balance = @totalLoan - @totalpaid

IF @balance = 0
BEGIN
UPDATE dbo.Loans
SET
    dbo.Loans.IsFinish = 1 -- bit 
	FROM dbo.Loans l INNER JOIN INSERTED i ON i.Loan_ID =  l.Loan_ID 
END

ELSE IF @balance >0
BEGIN
UPDATE dbo.Loans
SET
    dbo.Loans.IsFinish = 0 -- bit 
	FROM dbo.Loans l INNER JOIN INSERTED i ON i.Loan_ID =  l.Loan_ID 
END


END 

推荐答案

无事可做。什么EF阻止触发器工作没有什么特别之处。 EF就像你自己编写的任何其他代码一样执行查询或在查询窗口中输入。



我能想到你的触发器不起作用的唯一原因是如果您的触发器位于错误的对象上,或者您的触发器代码无法按您认为的那样工作。例如,在触发器SQL中,如果@balance小于零会发生什么?
There's nothing to do. There's nothing special about what EF does that prevents triggers from working. EF just executes queries just like any other code your write yourself or type in a query window.

The only reasons I can think of that your trigger wouldn't work is if your trigger is on the wrong object or your trigger code doesn't work the way you think it does. For example, in your trigger SQL, what happens if @balance is LESS than zero?


这篇关于插入使用实体框架后,为什么(SQL severtrigger)没有触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆