Is it possible to create a computed property in EF code first that can use the others elements of it's own table?
If no, what is the best way to do it without using a context method (i would like a property to bind it on the wpf UI without using converters)?
This example shows what i am trying to do (quietly easy to do in a computed sql field):
class MyEntity{
public virtual decimal Amount { get; set; }
public virtual decimal DivideOfThisAmountOnAll
{
get
{
var sum = *******.sum(e=>e.Amount); //How am i supposed to acces to the others?
return this.Amount / sum;
}
}
}
Instead of directly modifying your database object, I would recommend making a business logic layer for binding. Tools like https://github.com/AutoMapper/AutoMapper make it easy to transfer most of your table entities across without modification and then do things like create summary values, complex computed fields and other non-stored data tasks.
In MVC this is called Model, View, ViewModel, Controller, where the ViewModel is what you bind to and has all the computed results.
This does introduce additional complexity, but it leaves your Model objects alone (which is a good thing) and allows you to isolate your rules to one location (the ViewModel) which can avoid spreading the same computations across many controllers (which is an anti-pattern).