English 中文(简体)
如何利用4.0至4.3.1实体框架升级C#应用程序?
原标题:How do I upgrade a C# application using EntityFramework 4.0 to 4.3.1

我们有一个ASP.Net应用软件,这是利用实体框架4.0(这是.Net4.0的一部分)建立的。 在阅读了有关新特征的一些文章,最重要的是关于4.1、4.2和4.3产生的更清洁的SQL(SQL),我们决定借此机会更新我们的应用程序,以便使用4.3.1。

我用NuGet在应用程序中安装了4.3.1, 它成功地安装了 empleFramework. dll ; 它添加了一个引用, 当我建构时, 它被添加到文件夹中。 在运行时, 一切运行都顺利, 但是在 SQL 配置文件器中查看 SQL 配置文件, 并使用 Hibernatine Rhinos 的“ 实体框架剖析器 ”, SQL 似乎完全相同 。

NuGet所做的唯一一件事就是添加一个参考,我想我需要做点其他事情,迫使申请在运行时使用4.3.1号,但我无法确定我必须做什么。

我理解,SQL在正在处理的询问中可能完全相同,只要看一下产生的SQL可能不会显示任何差异,但我希望能够确认,新的版本正在运行时真正使用。

我是否需要在网络上添加其它内容 。 配置以确保4.3.1 被使用或者我所做的已经足够了 。 我当然需要改变某处的东西, 以便获得系统. data. 等东西, 而不是标准. Net4. 0 库, 以获得来自新实体Framerwork. dll 而不是标准. Net4. 0 库的功能 。

Any help gratefully received. Ste

问题回答

这是对的重申,“https://stackoverflow.com/uss/348971/meetjaydeep>>meetjaydeep s 回答。他和dpblog ,我从那里得到了大部分信息。

安装所描述的这里

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

EF4.0至EF 4.3.1的升级,如“a href='http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walktrough.aspx” rel=“nofollow noreferrer'>这里(我将跳到第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.

在我看来,这完全值得做额外的工作。 EF4.0对于它来说是太有限了。 EF 4.3.1 比较灵活,我喜欢所提供的综合食糖。 我确信,5.0 EF甚至更好,但我现在无法跳跃。

玩得开心点

要使 EF4.3.1 具有可评价性,您应该首先安装 EF4.1 更新1, 并使用最新的 NuGet 。

EF4.0是第一个或第一个模式的数据库,如果你想升级为EF4.3,最简单的方式是使用“代码生成器”:http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-mode-amp-database-patbase-first-walkentrough.aspx





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签