vs2012 mvc3项目ObjectContext与DBContext(转载)

安装好vs2012,开始学习vs2012下的mvc,但是在2010下熟悉的上下文基类ObjectContext不见了,取而代之的是DBContext,非常奇怪,一些熟悉的方法不见了,如:实体对象的Attach方法,上下文的ObjectStateManager对象,都不见了,奇怪,如何让上下文对象继承ObjectContext而不是DBContext,只能查资料了。mvc

百度了一天中文资料,没有找到,因而上谷歌搜英文资料,终于会找到了解决的方法,问题解决了。学习

 

问题:this

We move some projects from VS2010 to VS2012 but still using .NET 4.0 and not .NET 4.5spa

When I add an new EDMX, VS2012 use EF 5.0 but I'd like continue to use the version 4.x it's the first point..net

Second point the version 5.0, inherit from DbContext and not ObjectContext like version 4.x. With the version 5.0 I don't see any way to pass the connection string as parameter.设计

How can I use version 4.x in VS2012, with the same EDMX designer behavior ?rest

从老外的问题中能够看出,使用DBContext时,须要替换链接字符串仍是个问题呀,下面看看解决的办法吧:code

 

解决办法:对象

As mentioned in the comments, you're really getting EF 4.4, not EF 5.0. However, confusingly, EF 4.4 in contained in the NuGet EntityFramework 5.0 package.blog

For your other question, you can open the EDMX in the designer, restore "Code generation strategy" from "None" to "Default", then in the solution explorer, remove the .tt files. Now you'll have yourObjectContext again, and if you wish, you can remove the EntityFramework package from your project.

I'm not sure if there's any way to change this default behaviour of adding the NuGet package.

很简单了只须要在设计视图中打开EDMX文件,在属性面板中将“代码生成策略”改成“默认”,而后将解决方案管理器中的“.tt”文件删除,在查看.designer.cs文件,已经有生成的代码了,而且父类已是ObjectContext了,一切问题都OK了。

 

 

原文出处:http://blog.csdn.net/sweetsoft/article/details/8172299

相关文章
相关标签/搜索