2

We have an ASP.Net application which has been built using the Entity Framework 4.0 which is part of .Net 4.0. After reading a number of articles regarding the new features and, most importantly, the cleaner SQL generated by 4.1, 4.2 and 4.3, we decided to take the opportunity to upgrade our application to use 4.3.1.

I used NuGet to install 4.3.1 into the application and it succeeded in installing the EntityFramework.dll; it added a reference to it and, when I build, it is added to the bin folder. At runtime, everything runs through fine but, looking in SQL profiler and using the Entity Framework Profiler by Hibernatine Rhinos, the SQL appears to be identical.

As the only thing NuGet did was add a reference, I assume I need to do something else to force the application to use the 4.3.1 at runtime but I am unable to find out what I must do.

I appreciate that the SQL may well be identical for the queries being run and that just looking at the generated SQL may not be show any differences but I would like to be able to confirm that the new version is really being used at runtime.

Do I need to add something else to the web.config to ensure 4.3.1 is used or is what I have done enough? Surely I need to change something somewhere in order to get things like System.Data.Entity to come from the new EntityFramework.dll rather than the standard .Net4.0 libraries.

Any help gratefully received. Ste

stfinch
  • 21
  • 2

2 Answers2

6

This is a reiteration of meetjaydeep's answer with steps. Credit to him and to dpblog where I got a majority of this information from.

Install EF 4.3.1 as described here.

Installing EF 4.3.1
Please note that before you do the upgrade, this will temporarily break your code. So I suggest doing a backup before proceeding.

  1. Install NuGet if it is not installed already.
  2. Open the NuGet Package Manager Console (VS2010 Menu Bar > Tools > Library Package Manager)
  3. After it finishes loading execute this command, make sure to select the correct project from the drop down before hitting enter: Install-Package EntityFramework -Version 4.3.1

Upgrade EF 4.0 to EF 4.3.1 as described here (I would just skip to step 4).

Upgrading from EF 4.0 to EF 4.3.1
A word of caution - just because you installed EF 4.3.1, definitely does not mean you are finished. What you just did is just give yourself the option to use new templates (from what I have seen after doing this myself). Now is it is time to use those new templates.

  1. Open up your EDMX design view.
  2. On the design surface; Right Click > Add Code Generation Item
  3. Select "Online Templates" from the left menu
  4. Search for "DbContext"
  5. Select "EF 4.x DbContext Generator" from the list
  6. Name this item differently from your EDMX's name. "_____Model.tt" (fill in the blank). I used ___DBCModel.tt - Example: FooDBCModel.tt
  7. Click ‘Add’
  8. Verify that two files have been created: FooDBCModel.tt and FooDBCModel.Context.tt for example.

Fixing Your Now Slightly Broken Code
Your code won't compile now - don't despair - that's because what you just did is swap out System.Data.Objects.ObjectContext for the new and improved (well for me anyhow) System.Data.Entity.DbContext (Yaaaaay...)

  • You need to update all of your CUD (Create, Update, Delete) methods.
  • Instead of using context.AddToEntityNameHere(...) use context.EntityNameHere.Add(...)
  • Example: context.AddToProducts(product) > context.Products.Add(product)
  • You now have access to the Database property
  • You now have access to the Entry(...) method.
  • You can now explicitly state which properties to update during an update (context.SaveChanges()). Interested? Look here.

It is totally worth the extra work to do in my opinion. EF 4.0 was too limited for what it was. EF 4.3.1 is more flexible and I love the syntactical sugar that was provided. I am sure EF 5.0 is even nicer, but I can't make the leap right now.

Enjoy.

Community
  • 1
  • 1
dyslexicanaboko
  • 4,215
  • 2
  • 37
  • 43
  • Very well described steps but thers only one more precaution U need to take i.e in The Second Paragraph "Upgrading from EF 4.0 to EF 4.3.1" after the first step Double click on your .edmx file Select the Conceptual Entity Model and in the Properties window set "Code Generation Strategy" value to None . If U miss this step the code wont compile and complain about duplicate Entity Classess – Vipresh Apr 25 '13 at 13:13
  • 1
    That's strange, I never had that problem. Thanks for pointing it out though that never occurred to me. I took a look at my project and I have a feeling it might have been changed automatically because it is set to "none" already. This is something to look into for me. – dyslexicanaboko Apr 30 '13 at 15:45
2

To make EF4.3.1 avaliable, you should firstly install EF4.1 update1 and use the latest NuGet.

EF4.0 is database first or model first, If you want to upgrade to EF4.3, the easiest way is use "Code Generator": http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx

meetjaydeep
  • 1,752
  • 4
  • 25
  • 39
  • Thanks for the quick reply. Installing EF4.1 is something I did not know I needed to do - there were certainly no instructions regarding it anywhere around 4.3.1. I have installed it now thanks. However, I would still like to know how to prove that the new version is in place and is generating the SQL queries. – stfinch May 24 '12 at 13:58