安装好vs2012,开始学习vs2012下的mvc,可是在2010下熟悉的上下文基类ObjectContext不见了,取而代之的是DBContext,很是奇怪,一些熟悉的方法不见了,如:实体对象的Attach方法,上下文的ObjectStateManager对象,都不见了,奇怪,如何让上下文对象继承ObjectContext而不是DBContext,只能查资料了。
百度了一天中文资料,没有找到,于是上谷歌搜英文资料,终于会找到了解决的方法,问题解决了。
问题:
We move some projects from VS2010 to VS2012 but still using .NET 4.0 and not .NET 4.5
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.
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 ?
从老外的问题中可以看出,使用DBContext时,需要替换连接字符串还是个问题呀,下面看看解决的办法吧:
解决办法:
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.
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了。