vitm
vitm

Reputation: 525

Initialize EF POCO scalar properties in constructor

Are there any negative effects in creating a default constructor in my entity classes for initialization?

public User() {
    Id = Guid.NewGuid();
    DateCreated = DateTime.UtcNow;
    StateId = STATES.NEW.ID;
}

I want to do that to reduce code for any initialization of POCO in client classes like:

var user = new User() 
{
    Id = Guid.NewGuid(),
    DateCreated = DateTime.UtcNow, 
    StateId = STATES.NEW.ID 
};

Upvotes: 1

Views: 73

Answers (1)

DavidG
DavidG

Reputation: 118987

No there are no negative effects, as long as you have a public, parameterless constructor so EF is able to create objects. In fact, this is how you are supposed to create default properties with Entity Framework.

Upvotes: 1

Related Questions