开发者

Entity Framework POCO default constructor

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

publi开发者_运维百科c 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜