2

We are using the database first approach to creating our MVC models, which means the framework auto-generates a default constructor in the primary .cs file. I have a couple default values that I'd like to set, however, and the problem is this framework generates a basic .cs file for this model each time the .edmx is updated. Is there any way to either override this constructor in something like a partial class?

Example

public partial class Product
{
    // The framework will create this constructor any time a change to 
    // the edmx file is made. This means any "custom" statements will 
    // be overridden and have to be re-entered
    public Product()
    {
        this.PageToProduct = new HashSet<PageToProduct>();
        this.ProductRates = new HashSet<ProductRates>();
        this.ProductToRider = new HashSet<ProductToRider>();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NealR
  • 10,189
  • 61
  • 159
  • 299

2 Answers2

7

You could edit the t4 template that generates the classes to make it generate a partial method that is called in the parameterless constructor. Then you can implement this method in an accompanying partial class.

After editing, your generated code should look like this:

public Product()
{
    this.PageToProduct = new HashSet<PageToProduct>();
    this.ProductRates = new HashSet<ProductRates>();
    this.ProductToRider = new HashSet<ProductToRider>();
    Initialize();
}

partial void Initialize();

Now in your own partial class:

partial class Product
{
    partial void Initialize()
    {
        this.Unit = 1; // or whatever.
    }
}

The advantage over completely overriding the default constructor is that you keep EF's initialization code.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Going this route calls `Initialize` on already existing entities. I would like to implement something similar for new entities only (setting default values) – highboi Dec 08 '17 at 20:41
  • I know this answer was written almost 3 years ago, but I came across this problem and couldn't find where the T4 template was. I found 2 .tt files (`MyProjectModel.Context.tt` and `MyProjectModel.tt`) inside my project, but I modified them and when I updated the classes (right click in the emdx and Update from database), the added code was not generated. – Tales Dec 14 '18 at 15:13
  • @Tales The model generation is executed when the edmx is saved (except when this has been disabled explicitly). You can also trigger it by right-clicking a tt file in the solution explorer and selecting "run custom tool". – Gert Arnold Dec 14 '18 at 15:18
-1

as you can see the class that EF generates is public **partial** class. So create a new class and just add your code to it. Just make sure it has the same namespace as the EF generated file

//EF Generated
public partial class Product
{
}

//Custom class
public partial class Product
{
    // The framework will create this constructor any time a change to 
    // the edmx file is made. This means any "custom" statements will 
    // be overridden and have to be re-entered
    public Product()
    {
        this.PageToProduct = new HashSet<PageToProduct>();
        this.ProductRates = new HashSet<ProductRates>();
        this.ProductToRider = new HashSet<ProductToRider>();
    }

I should probably mention that your custom class should also be in a separate file.. I usually create a Metadata folder in the same directory as the edmx file and just add my partial classes in there

JamieD77
  • 13,796
  • 1
  • 17
  • 27
  • Anytime a change is made to the edmx file though, the constructor will be re-added to the `EF Generated`partial class, and an error thrown in the custom class saying `Memer with the same signature is already declared` – NealR Jan 28 '16 at 20:09
  • See this http://stackoverflow.com/questions/14485052/ef-5-model-first-partial-class-custom-constructer-how-to – Steve Greene Jan 28 '16 at 20:22
  • @NealR oh i see.. It didn't add constructors to the auto generated classes in the older versions – JamieD77 Jan 28 '16 at 20:42
  • 1
    @NealR there is a section at the top of the Model.tt T4 template you can take out if you don't want the tool to generate the constructor automatically. It also will add the default values in the constructor for you if you set the default values in the DB i believe – JamieD77 Jan 28 '16 at 20:47
  • This also does not work if your Entity (generated by the Wizard when you create or update the `.EDMX`) contains Navigation Properties. Because in such cases, the Wizard already generates a default parameterless constructor and adding a partial in the same namespace and writing your own constructor there causes an error `.ctor already defined`. – Shiva May 02 '17 at 00:46
  • @Shiva please see comment above yours about removing constructor creation from the t4 template if that's the case. – JamieD77 May 02 '17 at 14:31