1

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

2 Answers 2

3

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).
Sign up to request clarification or add additional context in comments.

1 Comment

yes C#, and that explains it. perfect. so many thanks to the both of you
1

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

1 Comment

Ops, posted it almost at the same time. You should also mention the language here, because I do believe how constructors created can differ from language to language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.