How to get property change notifications with EF 4.x DbContext generator

前端 未结 8 873
忘了有多久
忘了有多久 2020-11-27 18:29

I\'m playing around with Entity Framework 4.3, and so I am using the DbContext Generator to create the context and entity classes.

With the default EF 4 code genera

相关标签:
8条回答
  • 2020-11-27 19:27

    The solution from Anders above works, however there are a couple of gotchas I found during the process:

    In Step 1 where it says "Add the following to the CodeStringGenerator class", only the "public string Private(..." function can be added because the other two already exist. So, you need to find them and replace those two functions not add them, otherwise you'll get errors. To find exactly where you need to put them, do a search for "public class CodeStringGenerator", and look for the functions underneath that.

    In step 2 "Add the following to the generator", you only need to add the "using System.ComponentModel" line, and the lines from (and including) "public event PropertyChangedEventHandler...". Again, the other lines already exist, you will find them near the top of the .tt file.

    In step 3 "And a bit further down", both of those "foreach" loops also already exist, so they must be replaced and not added. Ultimately only one line is added to each foreach loop, "<#=codeStringGenerator.Private(edmProperty)#>" and "<#=codeStringGenerator.Private(complexProperty)#>" in each loop respectively.

    Also, don't replace the wrong loop, there are two additional loops above the ones you need to replace which both cycle through the same objects... make sure you replace the correct ones :-)

    I thought I would mention this because as an MVVM / EF novice (used to use NHibernate) I had to make these adjustments for it to work.

    0 讨论(0)
  • 2020-11-27 19:29

    You should continue using ObjectContext if you want property change notifications when you bind directly to the data model classes.

    The lightweight DBContext classes are for patterns such as MVVM or MVVMC where your view model implements property change notifications and your UI only binds to the view model's properties. You never bind to data model classes in these patterns.

    0 讨论(0)
提交回复
热议问题