ekkis
ekkis

Reputation: 10226

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

Upvotes: 1

Views: 655

Answers (2)

Andrew Savinykh
Andrew Savinykh

Reputation: 26290

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

Upvotes: 3

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364289

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

Upvotes: 1

Related Questions