I have defined a column in a SQL Server Table and set its value using an After Insert Trigger. This value is computed based on recently inserted identity. However, I am unable to get this value in the Controller after the database changes are saved.
Here is my (pseudo) code in the controller:
var model = new model();
model.column1 = abc;
model.column2 = xyz;
model.column3 = "CC";
// db object below is a model inheriting DbContext
db.table.Add(model);
db.SaveChanges();
// Do some operations not related to this table
// Try retrieving value of column3
// which should have been updated by the after insert trigger
var column3Value = db.table.find(model.id).column3;
What aspects of EF5 do I miss here?
Below is my trigger code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[SetColumn3]
ON [dbo].[Table]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON
UPDATE Table
SET Column3 = RTRIM(inserted.Column3) + CAST(inserted.Id + 100 AS nvarchar(10))
FROM inserted
WHERE Table.Id = inserted.Id;
END