Solved

Entity Framework 6 PropertyChange

Posted on 2014-03-16
8
2,785 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
Comment Utility
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
Comment Utility
I am Getting:

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

Expert Comment

by:Snarf0001
Comment Utility
Can you post the full code of the partial class?
Did you generate this with CodeFirst or ModelFirst?
0
 

Author Comment

by:moshik008
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now