开发者

ADO.NET fails to generate parameterless constructor

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 a开发者_JS百科bout 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


In C# (are you using C#?):

  • When you define no constructor in your class, a parameterless constructor will be created by compiler by default
  • When you define parameterless constructor (and maybe some others) the parameterless constructor will also be present as you defined it
  • When you define more than zero constructors, but no parameterless one, the compiler does not create a parameterless constructor for you. In this case it's your responsibility to define it (in partial class or not).


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜