Solved

Entity Framework 6 PropertyChange

Posted on 2014-03-16
8
2,965 Views
1 Endorsement
Last Modified: 2014-03-17
Hi,

I am using Entity FrameWork 6 with WPF(C#) and i am  extending the entity class with partial class to include business logic into it,
for example let's say i have 2 properties in the Entity "Salary" and "Months"
and i am adding a new property in the partial class "TotalIncome", when i am loading the data for the first time there is no problem to calculate TotalIncome = Salay*Months, but when i am updating one of the properties from the view i need to detect changes in "salary" or "Months" in order to recalculate "TotalIncome" .

thanks,

M
1
Comment
Question by:moshik008
  • 4
  • 4
8 Comments
 
LVL 22

Expert Comment

by:Snarf0001
ID: 39932857
Easiest way is to override the OnPropertyChanged event, check for the Salary and Months items and raise the event again on the custom property.

In the partial class where you're extending:

protected override void OnPropertyChanged(string property)
{
    base.OnPropertyChanged(property);
    if (property == "Salary" || property == "Months") OnPropertyChanged("TotalIncome");
}

Open in new window


That will fire change notifications WPF will pick up on anytime the 2 base properties are modified.
0
 

Author Comment

by:moshik008
ID: 39933492
I am Getting:

..OnPropertyChanged(string)': no suitable method found to override.....
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 39934043
Can you post the full code of the partial class?
Did you generate this with CodeFirst or ModelFirst?
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:moshik008
ID: 39934085
I am generating it with ModelFirst, and i saw somewhere that it supposed to include OnxxxChanged and OnxxxChanging for every property but not in generated project, any way not OnPropertyChanged either.
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 39934199
Ahh.  Previous versions of EF used a different code generation strategy, which hooked everything up.  I "believe" that due to the extra overhead, they eliminated this by default now.  

In my opinion, particularly with WPF, the benefits far outweight the extra overhead.

If you open the model designer, and go to Properties, check the "Code Generation Strategy".
I'm guessing it's set to "None".  If you change that to "Default", it should generate all of the change notification handlers.
0
 

Author Comment

by:moshik008
ID: 39934215
MAGIC!! works! thanks a million.
before i am accepting your solution, in VS 2013 the "NONE" option changed to "Legacy Object Context" and the default is "T4"   it says that the Legacy Object is not the right way to go?
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 39934247
I don't have VS2013, still using 2012.  But, if you have time, would suggest looking into the t4.
Basically gives you more control over how / what you're putting in for each class.

You can get INotifyPropertyChanged implementation with it, but I honestly just haven't been bothered to yet.  Probably stupid of me, but old way is sufficing for my needs.
0
 

Author Closing Comment

by:moshik008
ID: 39934251
Thank you!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question