3

I have following problems.

I use a trigger to do Statistic for my website.

In my Controller I use this code:

 db.MyModel.Add(model);
 db.SaveChanges();

My trigger work well when I insert records in SQL Server Management Studio. But when I insert records from website, my trigger do not fire.

I have similar trigger on other table and all of them work well. I don't know why only this table cause the problem. I also tried to delete edmx file and create it again but still no luck. Is there something that I need to do to make it works?

I'm using Entity Framework 5, MVC4, Visual Studio 2012, MSSQL Server 2008 R2.

Thank you.

UPDATE #1: When insert records from website, new records are saved to database but trigger do not occurs. Program do not generate any errors.

3
  • 2
    Entity Framework doesn't have any magic that lets it bypass triggers. Try running SQL Profiler to see what commands are hitting the database. Commented Sep 18, 2013 at 4:02
  • 1
    It would be helpful to see the results of the SQL trace from Profiler. It would also help to see the content of the trigger itself. Commented Sep 18, 2013 at 4:07
  • I haven't do any research on Profiler. So please wait a bit. I need to google and will post the results of SQL Trace if I still can't find out the problem. Thank you all :) Commented Sep 18, 2013 at 4:13

3 Answers 3

1

I had the same problem using Entity Framework with SQL Server Database.

For the trigger to be fired on the database you need to send the 'commit' using 'TransactionScope' after 'db.SaveChanges();' command.

I am using the Entity Framework Core library. Here is an example:


using (var ts = new TransactionScope())
{
    using (var db = new DbContext()) // Modify to you Context Class
    {
        // Your logic code
            
        db.MyModel.Add(model);
        db.SaveChanges();
        
        // commit
        ts.Complete();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is expected behaviour when using a transaction scope. Depending on how the trigger is set up, it really cannot fire until after the transaction is committed.
1

If an UPDATE or INSERT INTO statement occurs in SQL Server, the trigger will fire.

Your code, as written, does not directly perform an update unless you have actually changed some data before db.SaveChanges();

The only other possibility is that the trigger is being disabled prior to the DML operation, and enabled afterwards. See DISABLE TRIGGER ... in the Docs.

4 Comments

May be my information is not very clear. Model in "db.MyModel.Add(model);" is a new entity create from web form. And new records is successully inserted to database. That mean INSERT INTO Statement already occurs, right? Sorry I'm new to this :(
Either your trigger is not configured properly, or you are not inserting records into the table. Can you use SQL Server Management Studio to view the records using SELECT * FROM [TableInQuestion] to see the records you inserted with the web app?
Yes, the record is inserted. But the trigger still can not fire. I also try to create new trigger on that table and it still not fire after insert by web app. (Insert directly in database still work). Another Trigger after UPDATE still work on web app.
Update your question with the trigger code and use Profiler to grab the code generated by Entity Framework. Triggers are not optional, there is no way to prevent a trigger from running -> either the trigger is incorrectly written, or data is not being inserted.
0

EF uses sp_executesql('query_string') statement for querying. Triggers are not fired in this case.

EDIT: @Gert Arnold - Yes, you are right in your comment. I have pasted wrong link, but caould not find correct anymore. To be fair I have removed link, but my answer is correct. Even in SSMS using sp_executesql('query_string') will not fire triggers on table. Workaround is creating a stored procedure with needed operations and call it from EF like this way:

dbContext.Database.SqlQuery<YourModel>("StoredProcedureName",params);

Triggers will be fired as expected.

3 Comments

Wrong! The link doesn't state a feature but a bug.
In every hit I find on sp_executesql + "no trigger fired" it's always faulty (or downright stupid) trigger code that causes the error. It's highly unlikely (I dare say impossible) that sp_executesql wouldn't fire triggers. The only thing that comes close is here but that's a different issue.
@Gert Arnold if trigger is fired without error in standard query in SSMS, so it's code is OK... But if you will find any solution or reason why it's not fired, please leave a comment here - i will be happy to learn sth new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.