6

I intend to extend the constructors of some of the entities in my Entity Framework (4).

However how do I ensure that my constructor is run after the model has run its. i.e. I want to ensure that the the object holds the data from the database before I work on it in my constructor.

Adam
  • 291
  • 3
  • 13
  • 6
    You should consider marking some answers on your (many) questions. This will probably help you receive more quality responses. Just hit the check-mark near the appropriate answers to your questions. – Reed Copsey Jun 15 '10 at 23:53
  • Don't know about his other questions, but this one doesn't have an answer to it.... – Hector Minaya Dec 06 '10 at 03:32
  • possible duplicate of [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) - there's a great solution there involving the editing of the T4 templates. – Rob Church Jul 18 '13 at 11:00

2 Answers2

3

There is no generated constructor besides the default one; the objects are created via a factory method, and are simply initialized after construction.

You can write your own default constructor, and the generated code will call it before initializing all of the generated properties. If you write your own non-default constructor, you will also have to write your own default constructor, or else the designer file will not compile, since it assumes a default constructor exists.

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
1

Use constructor chaining.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • 2
    Thanks Pavel Constructor chaining I don't think will help. The entity framework creates : partial class ABC { ABC() {} } I create : partial class ABC { ABC() { do something here } } I want my constructor to be called after the entity framwork one .. so that the object is populated with data so I can 'do something here' with it .. – Adam Jun 16 '10 at 00:47
  • doesn't layout nicely in this comment area .. sorry – Adam Jun 16 '10 at 00:49
  • 1
    You can't do that, sorry. You can't have half of a method (or constructor) in one place, and another half of the same method (or constructor) in another place - it's not what partial classes/methods do. – Pavel Minaev Jun 16 '10 at 04:32
  • That said, I wonder where do those generated constructors come from in the first place. I just played with a simple EF4 project a little bit, and code generated for my entities did not have any explicitly declared constructors... – Pavel Minaev Jun 16 '10 at 04:34
  • For a one-to-many relationship in the database EF generates a list-type (ICollection) property. Those are initialized in a constructor to an empty list. – Hans Kesting Apr 25 '14 at 09:36
  • 1
    Also, if you define any default values, the Entity Framework won't generate SQL constraints for it, but it'll assign the default values in the constructor of the entity. My problem is that I added some transient (non-persisted) properties to the entity, and I want to initialize that too, just like the generated property initializations. – Csaba Toth Oct 04 '15 at 06:40