Sheldon Cooper
Sheldon Cooper

Reputation: 627

How to efficiently set default entity values in Entity Framework 6, Database First

I am new to Entity Framework and have only worked with database first development so far. I have been reading Entity Framework 2E which focuses on EF4. The generated classes in EF4 had no parameterless constructor so I was able to define partial classes and initialize entity properties in the parameterless constructors I defined there. The other options were to edit the EDMX directly and set the StoreGeneratedPattern attribute of a property to Computed or set the Default Value attribute of the property. The problem with the latter 2 options is that they'd be wiped out whenever I'd Update From Model. The parameterless constructors in partial classes worked nicely and were never wiped out.

Now in EF6, the partial classes look like POCOs and are not tightly coupled to EntityObject. However, they define parameterless constructors. Is there a way to set default values or initialize object properties on creation that won't get wiped out when I update the model?

I know I can resort to event handlers like Inserting on a data source, but I'd like to just set it globally.

Thanks

Upvotes: 2

Views: 901

Answers (1)

Sheldon Cooper
Sheldon Cooper

Reputation: 627

Okay I found the solution. Edit the template to input a partial method definition and a call to it in the constructor, then implement the partial method in a partial class.

Full explanation found here: https://www.youtube.com/watch?v=i8J2ipImMuU

Upvotes: 2

Related Questions