-1

I looked around on how to do this (see here) but I still am not clear on how to create constructors for generated entity classes?

How do I do that?

Chris K.
  • 153
  • 2
  • 12

1 Answers1

0

CodeCaster has the right idea.

If your entity classes are partial, you can create another piece of the class that extends the definition of the entity into a file that will not be regenerated every time you save the edmx.

Keep in mind that when you make updates to the edmx from your database (table changes, column type changes etc.) you may need to revisit the partial class file you defined to make sure that it works correctly with the new changes, because EF won't have any context for that extension of the entity class.

Josh Gust
  • 4,102
  • 25
  • 41
  • But EDMX-generated, partial entity classes will already come with a non-partial constructor to initialize navigation properties, or an empty one if no navigation properties exist. That's why I commented, and did not post an answer. **If** these classes are generated from an EDMX, the answer is actually in the duplicate [EF 5 Model First Partial Class Custom Constructer How To?](http://stackoverflow.com/questions/14485052/ef-5-model-first-partial-class-custom-constructer-how-to). – CodeCaster Jan 06 '16 at 18:48
  • That's a very good point. I was under the impression that the OP didn't necessarily want to extend any existing constructors, but rather extend the entity with completely new constructors. – Josh Gust Jan 07 '16 at 20:52
  • 2
    Yeah so that depends on what OP wants. When that is parameterless constructors, you need to change the templates. – CodeCaster Jan 07 '16 at 20:54
  • @CodeCaster is correct except I don't believe Designer generates a parameterless constructor when there are no navigation properties or they are 0..1 relations (ie: not a collection) – CervEd Sep 08 '20 at 08:01