Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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:

SQL
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 
Posted
Updated 18-Dec-16 4:26am
Comments
Mohtshm Zubair 18-Dec-16 4:12am    
initially triggers are not active or enable. So select from Object explorer and make them active
Ahmed Dabas 18-Dec-16 4:34am    
The trigger already enabled

1 solution

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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900