ADO.NET 无法生成无参数构造函数
我创建了一个名为 EF 的 ADO.NET 模型,并添加了一个 DbContext 生成器,该生成器使用 EF.tt 和 .cs 文件填充我的 /Model 文件夹,每个实体一个。
一般来说,系统创建带有无参数构造函数的类...出于某种原因,我无法理解我有一个缺少此构造函数的实体。它不是抽象类,没有基类型并且具有公共访问权限。我有很多其他这样的类,但它们都有无参数构造函数。我用谷歌搜索并浏览了 VS,试图弄清楚这个有什么特别之处,以及如何让它生成构造函数,但没有找到答案。
我总是可以在部分定义中创建它,但我宁愿弄清楚它。另外,如果我在 EF.tt 上单击鼠标右键,我会在菜单中看到一个名为“运行自定义工具”的选项,但当我选择它时似乎什么也没有发生。如何重新生成 .cs 文件?
ps 是的,我已经清理并重建了解决方案,以防它被搞乱但仍然存在问题
I've created an ADO.NET model called EF and added a DbContext generator, which populates my /Model folder with an EF.tt and .cs files, one for each entity.
in general the system creates classes with parameterless constructors... for some reason I can't fathom I have an entity that's missing this constructore. It is not an abstract class, has not base type and has public access. I have tons of other such classes but they all have parameterless constructors. I've googled and looked around VS trying to figure what's special about this one, and how I can make it generate the constructor, but find no answer.
I can always create this in a partial definition but I'd rather figure it out. Also, if I right-mouse click over the EF.tt I see a choice in the menu called "Run Custom Tool" but when I select it nothing seems to happen. How does one regenerate the .cs files?
p.s. yes, I have cleaned and rebuilt the solution in case it just got messed up but still problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C# 中(您使用 C# 吗?):
In C# (are you using C#?):
默认构造函数默认存在,但不生成。如果类没有任何显式定义的构造函数,则它始终具有默认的无参数构造函数。如果您在其他地方指定了任何构造函数(部分类),则默认无参数构造函数将不再存在,如果您想使用它,则必须自己创建它(EF 总是想使用它)。
Default constructor exists by default, it is not generated. If class doesn't have any explicitly defined constructor it always have default parameterless constructor. If you specify any constructor elsewhere (partial class) default parameterless constructor doesn't exist any more and you have to create it yourselves if you want to use it (EF always wants to use it).