8

I built my EF Model in EF 4.0, and then installed the 4.1 upgrade that includes the new DBContext interface. How do I update my model so that it uses the 4.1 features going forward?

Thank You

Ben Finkel
  • 4,753
  • 7
  • 34
  • 49

2 Answers2

13

You can use DbContext with your EDMX model. After installing EFv4.1 you should have new T4 template available: DbContext generator. This will take your EDMX and create context derived from DbContext and all POCO entities for you. Here you have walkthrough.

But if you want to switch to DbContext just because of DbContext.Entry.State you don't have to. EFv4 has a similar mechanism:

context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

Here is the full description how to update an entity in EFv4.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • This is great info and points me in exactly the right direction. Thank you!! – Ben Finkel Apr 14 '11 at 18:45
  • Hi Ladislav Mrnka, I've installed EFv4.2 via Nuget but I still don't see "DbContext generator" T4 template in EDMX model, only "Entity object generator" and "Self-tracking generator" are available. Do I need to install EFv4.1 first ? – JatSing Nov 16 '11 at 07:31
  • 1
    @Sun: DbContext generator is not part of EFv4.2 because NuGet package doesn't install templates. You must download it separate from Visual Studio gallery. – Ladislav Mrnka Nov 16 '11 at 10:56
0

What beneftis are you hoping to see by upgrading from EF4.0 to 4.1? You're obviously not going to benefit from using model-first development since you already have an existing model. You can already generate POCO objects from EF4.0. See Entity Framework upgrade from v4 to v4.1(RC)

Community
  • 1
  • 1
Joel C
  • 5,547
  • 1
  • 21
  • 31
  • Specifically, every tutorial I can find online for updating models uses the DBContext.Entry.State mechanism. I figured the easiest thing to do would be to use the latest version which has this handy function for doing so. – Ben Finkel Apr 14 '11 at 17:51
  • 1
    In fact, I can't find anything about EF 4.0 and how to work with it. The message seems largely to be: use 4.1 and forget 4.0 ever existed lol – Ben Finkel Apr 14 '11 at 18:35