I am Using C# 2010 Entity framework code first
If I have A class like this
partial class test
{
public double value1;
public double value2;
public double value3;
public double totals;
}
That generated by code
how could I create partial class to calc totals
partial class test
{
public double totals { get { return value1 + value2 + value3; } }
1 – you know it is not practical to change the generated class.
2 – there is no way to partially define the property totals.
3 – And using metadata class does not update totals till savechanges.
I’ll appreciate If anyone could define how can I implement OnpropertyChanged and used to solve this problem
Thanks
Your property in the second class is missing the “get” keyword:
Alternatively you could define a Total() method.
As far as I can tell from your question (“how could I create partial class to calc totals”), you don’t need to use OnPropertyChanged because the totals property/method will run the calculation each time with the new values in totals. You can remove that field from the original class as it’s a calculation and really shouldn’t be stored.
If you must actually update a total value in the database you can use the OnPropertyChanged partial methods as you suggested. You’ll need to use properties to call the new method:
You should make the original fields private and only provide public access through the property. This is not only better code design but also allows you to implement the kind of functionality you desire.
There are, of course, other patterns that you can follow, but this should get you started.