I have an SQL Server table called “MyTable” with 3 integer columns: column1, column2 and column3. column3 is a computed column, evaluating the expression:
case
when column1>column2 then 1
else 2
end
Let’s say that my table has only one row with values (300, 50, 1)
Now suppose that i have the following LINQ code:
MyTable t = db.MyTables.First();
t.column1=5;
Console.Write("Computed Value = " + t.comlumn3);
db.SubmitChanges();
What will the written output be? 1 or 2? In other words, is the value of the computed column auto-updated in my code, even before I “Submit Changes”;
Thank you in advance.
No, computed columns are (when declared in the database as you do) only evaluated by the database (ie it will only have the correct value after a save/reload).
If you want it immediately evaluated in the code and don’t really want the computed column stored in the database, you could change column3 in your datatype into a property that calculates the value in the getter.
If you’re using EF, you’ll want to add
[NotMapped]to your added property to avoid EF trying to store it in the database.