SiberianGuy
SiberianGuy

Reputation: 25312

Entity Framework POCO default constructor

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?

Upvotes: 7

Views: 2076

Answers (1)

Kit
Kit

Reputation: 21709

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.

Upvotes: 7

Related Questions