How do SQL Server computed columns work in EntityFramework?

Deadly 提交于 2020-01-11 09:51:32

问题


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.


回答1:


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):




回答2:


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.



来源:https://stackoverflow.com/questions/12625453/how-do-sql-server-computed-columns-work-in-entityframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!