Kishore Kumar
Kishore Kumar

Reputation: 12884

How do SQL Server computed columns work in EntityFramework?

I have a table whose column TotalAmount is a computed column from columns Amount+Extra, so how this computation works, if I use Entity Framework on this table?

Thanks.

Upvotes: 3

Views: 2153

Answers (2)

Craig W.
Craig W.

Reputation: 18175

In addition the response by marc_s it is important to note that changing the underlying properties of the EF entity will not recalculate the value unless you save the object and then refresh it from the database. Put another way, the property on the C# object does not implement the calculation from the database.

Upvotes: 3

marc_s
marc_s

Reputation: 755351

A computed column is either (a) recalculated every time you access it, or (b) if you defined it with the PERSISTED keyword, an actual column is created in your table's data pages and the value is stored there.

In any way, to Entity Framework, both kinds of computed columns should behave just like regular columns. If Entity Framework accesses the table and reads data from it, it should get the current value of TotalAmount and store it in your entity's corresponding property.

When you create an ADO.NET Entity Data Model from a database, EF will pick up that the column is a computed column, and thus you won't be able to set a new value for that property (obviously):

enter image description here

Upvotes: 2

Related Questions