1

Can't get AFTER INSERT trigger to fire after SaveChanges() was done. Using T-SQL with ExecuteStoreCommand() doesn't help. All triggers are fired on the server, but not from the Entity Framework.

What I am doing: I create a new row in parent table:

ORDERS order = new ORDERS
{
   GUID = Guid.NewGuid().ToString(),
   ORDERDATE = DateTime.Now,
};
DB.AddToORDERS(order);
DB.SaveChanges();

And finally I put some data to the child table where the trigger must be fired after insert:

foreach (SPAREPART item in SOMECOLLECTION)
{
   ORDERITEMS orderitem = new ORDERITEMS
   {
      ORDER_ID = order.ORDER_ID, //here order.ORDER_ID is 0, but why?
      PRICE = item.INCOME_PRICE
   };

   DB.ORDERITEMS.AddObject(orderitem);
   DB.SaveChanges();
}

The trigger simply updates parent table:

AS
begin
  UPDATE ORDERS o SET o.sum = o.sum + NEW.price WHERE o.order_id = NEW.order_id;
end

Also I can't get the increased order.ORDER_ID generated by firebird generator, it is always 0 after SaveChanges() was done.

My DB schema is generated out of FireBird database.

desperate man
  • 905
  • 1
  • 17
  • 39

1 Answers1

0

If your ORDER_ID is 0 it means that it is not configured to be returned back from the database after insert. Check that your ORDER_ID property is configured with StoreGeneratedPattern.Identity.

It is also reason why your trigger doesn't work. It is fired but for not existing ORDER_ID (0).

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • applied StoreGeneratedPattern.Identity and now order_id is updating by itself. unfortunately trigger still doesn't work. – desperate man Jul 21 '11 at 13:34
  • Can you capture SQL executed by EF? How is it different from your manual SQL which executes trigger? – Ladislav Mrnka Jul 21 '11 at 21:08
  • Can't have rights to see DB server profiler and IntelliTrace didn't show me any ADO.NET debug trace (I'm newbie in EF). Anyway just tried to overcome my problem by using official Firebird .NET provider without EF and the damn trigger still doesn't work. Perhaps if I will not find answer I will move AFTER INSERT trigger to BEFORE INSERT or update my fields manually. – desperate man Jul 22 '11 at 08:47