安裝好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了。