0

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
4

1 回答 1

1

Column3DatabaseGeneratedOption.Identity如果要取回触发器在插入期间创建的数据,则必须定义属性。此声明告诉 EF 该值是在插入期间在数据库中创建的,并且它必须查询该值。

于 2013-03-17T14:31:18.817 回答