简体   繁体   中英

How do I upgrade a C# application using EntityFramework 4.0 to 4.3.1

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM