?
Solved

Entity Framework 6 PropertyChange

Posted on 2014-03-16
8
Medium Priority
?
3,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 23

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 23

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 23

Accepted Solution

by:
Snarf0001 earned 2000 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 23

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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…
Six Sigma Control Plans

771 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