Daniel Santos Daniel Santos - 1 year ago 74
C# Question

ASP.Net MVC code first. How force a get only attrubute to be stored on db?

The fallowing attribute is defined on a POCO class.

public int total { get{ return sub_total_1 + sub_total_2; }}

when a new migration is added this attribute is not created nor stored on the table.

I tried to add [Column("Total")] annotation without success.

Is possible to make this attribute to be stored on db as a "cache" for future queries?


Answer Source

For EF to be able to monitor a property, it needs to have a public (1) getter and setter.

For something like what you're doing, unless you need it in the database for some operations there, you're fine just leaving it as a readonly property in your Poco class. The operation of adding two integers together is not at all a performance worry, and would probably be slower to push and pull from the database.

(1) The setter can be protected internal if you want it to be "read only" except for the database.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download