首先是Entity Framework 5模型-IDisposable哪里去了? [英] Entity Framework 5 model first - Where is IDisposable gone?

查看:116
本文介绍了首先是Entity Framework 5模型-IDisposable哪里去了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先在Entity Framework 5模型中,似乎由于类文件的生成方式而发生了一些重大变化(不再生成代码,而是T4模板)

In Entity Framework 5 model first, there seem to be some breaking changes due to the way the class files are generated (No more code generation, but T4 templates)

2个示例:

  • 生成的上下文文件不再实现IDisposable
  • 没有构造函数可以再使用连接字符串

还有更多重大变化吗?对他们有什么解决方案?

Are there more breaking changes? And what is the solution to them?

推荐答案

从Entity Framework 5中的模型生成的默认代码现在继承了DbContext而不是ObjectContext.

The default code generated from a model in Entity Framework 5 now inherits DbContext instead of ObjectContext.

这仍然实现IDisposable,但是如果您从类似于以下代码的一行中得到错误:

This still implements IDisposable, but if you're getting an error from a line of code similar to this:

using (var mymodel = new MyModelContext()) { ... }

...抱怨没有实现IDisposable,那么您的问题很可能是您的模型是在引用EF5的单独程序集中定义的,而您尚未向项目添加EF5引用.

...complaining about not implementing IDisposable, then your problem is most likely that your model is defined in a separate assembly that references EF5 and you have not added an EF5 reference to your project.

正如拉迪斯拉夫·姆恩卡(Ladislav Mrnka)在他的回答中已经提到的那样,如果要将连接字符串传递给构造函数,则必须手动创建自己的构造函数.

As Ladislav Mrnka has already mentioned in his answer, if you want to pass a connection string to the constructor you have to create your own constructor manually to do this.

如果您想将Entity Framework切换回生成的代码的较旧样式,它将自动生成您要查找的构造函数,请按照以下步骤操作:

If you want to switch Entity Framework back to the older style of generated code, which will automatically generate the constructor you're looking for, then follow these steps:

  1. 单击EDMX文件的设计器图,然后查看属性窗口.找到一个名为代码生成策略"的属性,并将其设置为默认",而不是无".这将告诉Visual Studio在一个大文件中的MyModel.Designer.cs中开始为您的对象模型创建代码,这次使用ObjectContext而不是DbContext.
  2. 从EDMX文件下面删除以下子文件:MyModel.Context.tt,MyModel.tt.这些是您不再需要的自动生成的文件.如果不删除它们,则将导致类命名冲突,因为对象将被创建两次.

这篇关于首先是Entity Framework 5模型-IDisposable哪里去了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆