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>();
    }
}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Going this route calls Initialize on already existing entities. I would like to implement something similar for new entities only (setting default values)
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 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".
-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

6 Comments

Anytime a change is made to the edmx file though, the constructor will be re-added to the EF Generatedpartial class, and an error thrown in the custom class saying Memer with the same signature is already declared
@NealR oh i see.. It didn't add constructors to the auto generated classes in the older versions
@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
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.