Solved

Entity Framework 6 PropertyChange

Posted on 2014-03-16
8
3,106 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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 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 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is about my first experience with programming Arduino.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

691 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