Solved

Entity Framework 6 PropertyChange

Posted on 2014-03-16
8
2,914 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 20
Hey!! 5 33
Switching Visual Studio 2015 to normal debug display 3 25
printf performancy 11 33
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 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…

785 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