live-love
live-love

Reputation: 52386

Entity Framework - How to Modify generated base constructor (DBContext)

We have to deal with production and test connection strings in our environment. Database First Solution.

I have an extremely picky client that is not happy with the fact that you can create a partial class with a second constructor with a parameter, or inherit from the named Entities class with an empty parameter constructor.

He claims that a developer could unknowingly use the base constructor.

Is there any way to modify the generated constructor, or set an option so that the base constructor does not get generated, so we can write our own?

Thanks!

Upvotes: 2

Views: 2763

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364299

If you are using T4 template for context generation you can do whatever you want. For example:

  • Make your context sealed
  • Remove partial keywork from generated context class
  • Define constructor you want directly in the template

The only thing you need to do is modify the ModelName.Context.tt template.

Anyway your client should concentrate on business requirements and not on stupid assumption about coding.

He claims that a developer could unknowingly use the base constructor.

I claim that this can happen but it is not an issue if your application is correctly tested and if you make code review for new team members or junior developers.

Upvotes: 3

Related Questions