7

Is it okay to have default constructor which sets some default values like:

public class BetScreenshot
{
   ...

   public BetScreenshot()
   {
       CreationDateTime = DateTime.UtcNow;
       StatusEnum = BetScreenshotStatus.NotProcessed;
   }
}

My first bad feeling is that these properties might be marked as modified during EF entities instantiation. But may be there is something else?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • This doesn't qualify as a [POCO entity](http://msdn.microsoft.com/en-us/library/dd468057.aspx), you need to have a parameterless constructor. Additionally, one can't tell if you are enabling change tracking because the property declarations are not visible (they need to be `virtual` if you want change tracking enabled). – casperOne Aug 25 '11 at 12:52
  • @casperOne The constructor looks parameterless to me...? – Mike Nov 17 '11 at 21:57
  • @Mike: Look at the [edit history](http://stackoverflow.com/revisions/7190472/3) in relation to when the comment was made. – casperOne Nov 18 '11 at 01:54

1 Answers1

7

Yes it's ok to initialize properties. Effectively during construction using a parameterless constructor, the fields of the type are initialized to the default anyway. You're just choosing a different default. It's a pretty common practice to new up child entities and collections, but there's no reason simple properties can't be initialized. I do this for several entities and EF correctly recognizes the object as new/unmodified.

Community
  • 1
  • 1
Kit
  • 20,354
  • 4
  • 60
  • 103