vs2012 mvc3项目ObjectContext与DBContext(转载)

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

 

 

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

转载于:https://www.cnblogs.com/assur/p/3457810.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值