简体   繁体   English

如何使用EntityFramework 4.0将C#应用程序升级到4.3.1

[英]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. 我们有一个ASP.Net应用程序,它是使用Entity Framework 4.0构建的,它是.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. 在阅读了大量有关新功能的文章后,最重要的是,4.1,4.2和4.3生成的更清晰的SQL,我们决定借此机会升级我们的应用程序以使用4.3.1。

I used NuGet to install 4.3.1 into the application and it succeeded in installing the EntityFramework.dll; 我使用NuGet将4.3.1安装到应用程序中,并成功安装了EntityFramework.dll; it added a reference to it and, when I build, it is added to the bin folder. 它添加了对它的引用,当我构建时,它被添加到bin文件夹中。 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. 在运行时,一切都运行良好但是,查看SQL分析器并使用Hibernatine Rhinos的Entity Framework Profiler,SQL看起来是完全相同的。

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. 由于NuGet做的唯一事情就是添加一个引用,我假设我需要做一些其他事情来强制应用程序在运行时使用4.3.1,但我无法找到我必须做的事情。

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. 我很欣赏SQL对于正在运行的查询可能完全相同,只是查看生成的SQL可能没有显示任何差异,但我希望能够确认新版本确实在运行时使用。

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? 我是否需要在web.config中添加其他内容以确保使用4.3.1或者我已经做得足够多了? 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. 当然,我需要在某处更改某些内容,以使System.Data.Entity等内容来自新的EntityFramework.dll,而不是标准的.Net4.0库。

Any help gratefully received. 任何帮助感激不尽。 Ste STE

This is a reiteration of meetjaydeep's answer with steps. 这是对meetjaydeep的回答的重复。 Credit to him and to dpblog where I got a majority of this information from. 感谢他和dpblog ,我从中获得了大部分信息。

Install EF 4.3.1 as described here . 按照此处的说明安装EF 4.3.1。

Installing EF 4.3.1 安装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. 如果尚未安装NuGet,请安装它。
  2. Open the NuGet Package Manager Console (VS2010 Menu Bar > Tools > Library Package Manager) 打开NuGet包管理器控制台(VS2010菜单栏>工具>库包管理器)
  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 完成加载后执行此命令,确保在输入之前从下拉列表中选择正确的项目: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). 按照此处的说明将EF 4.0升级到EF 4.3.1(我将跳到第4步)。

Upgrading from EF 4.0 to EF 4.3.1 从EF 4.0升级到EF 4.3.1
A word of caution - just because you installed EF 4.3.1, definitely does not mean you are finished. 需要注意的是 - 仅仅因为你安装了EF 4.3.1,绝对不是说你已经完成了。 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. 打开EDMX设计视图。
  2. On the design surface; 在设计表面; Right Click > Add Code Generation Item 右键单击>添加代码生成项
  3. Select "Online Templates" from the left menu 从左侧菜单中选择“在线模板”
  4. Search for "DbContext" 搜索“DbContext”
  5. Select "EF 4.x DbContext Generator" from the list 从列表中选择“EF 4.x DbContext Generator”
  6. Name this item differently from your EDMX's name. 将此项目命名为与EDMX名称不同。 "_____Model.tt" (fill in the blank). “_____Model.tt”(填空)。 I used ___DBCModel.tt - Example: FooDBCModel.tt 我使用___DBCModel.tt - 示例:FooDBCModel.tt
  7. Click 'Add' 点击“添加”
  8. Verify that two files have been created: FooDBCModel.tt and FooDBCModel.Context.tt for example. 验证是否已创建两个文件:例如FooDBCModel.tt和FooDBCModel.Context.tt。

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...) 你的代码现在不会编译 - 不要绝望 - 那是因为你刚刚做的是换掉System.Data.Objects.ObjectContext用于新的和改进的(对我来说无论如何)System.Data.Entity.DbContext(Yaaaaay。 ..)

  • You need to update all of your CUD (Create, Update, Delete) methods. 您需要更新所有CUD(创建,更新,删除)方法。
  • Instead of using context.AddToEntityNameHere(...) use context.EntityNameHere.Add(...) 而不是使用context.AddToEntityNameHere(...)使用context.EntityNameHere.Add(...)
  • Example: context.AddToProducts(product) > context.Products.Add(product) 示例:context.AddToProducts(product)> context.Products.Add(product)
  • You now have access to the Database property 您现在可以访问Database属性
  • You now have access to the Entry(...) method. 您现在可以访问Entry(...)方法。
  • You can now explicitly state which properties to update during an update (context.SaveChanges()). 您现在可以显式声明在更新期间要更新的属性(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.0太有限了。 EF 4.3.1 is more flexible and I love the syntactical sugar that was provided. EF 4.3.1更灵活,我喜欢提供的语法糖。 I am sure EF 5.0 is even nicer, but I can't make the leap right now. 我相信EF 5.0更好,但我现在无法实现飞跃。

Enjoy. 请享用。

To make EF4.3.1 avaliable, you should firstly install EF4.1 update1 and use the latest NuGet. 要使EF4.3.1可用,首先应安装EF4.1 update1并使用最新的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 EF4.0是数据库优先或模型优先,如果你想升级到EF4.3,最简单的方法是使用“代码生成器”: http//blogs.msdn.com/b/adonet/archive/2011/09/ 28 / EF-4-2-模型放大器数据库先walkthrough.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么要升级到C#4.0? - Why should I upgrade to c# 4.0? 如何使用C#4.0编写可扩展的套接字服务器? - How do I write a scalable socket server using C# 4.0? 如何使用CSharpCodeProvider类运行C#4.0编译器? - How do I run C# 4.0 compiler with CSharpCodeProvider class? 如何正确使用 C# 8 和 .NET 4.0 - How do I properly use C# 8 with .NET 4.0 如何在 C# 中找到已安装应用程序的升级代码? - How can I find the upgrade code for an installed application in C#? 如何在 C# Selenium 项目上升级 ChromeDriver? - How do I upgrade ChromeDriver on a C# Selenium project? C#应用程序EntityFramework CodeFirst和数据库 - C# Application EntityFramework CodeFirst and database 如何使用C#4.0从Windows应用程序的客户端中读取服务器中的文本文件 - how to read text file in server from client in windows application using C# 4.0 如何解决“Package Google Authenticator 1.2.1 was restore using .NETFramework”,但我不想升级到最新版本(C#) - How to resolve “Package Google Authenticator 1.2.1 was restored using .NETFramework”, but I do not want to upgrade to latest version (C#) 如何在.NET 4.0 C#WinForms应用程序中播放SWF文件? - How to play SWF file in .NET 4.0 C# WinForms application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM