Disable the default constructor on a Database-First workflow

You can modify the template to provide the constructor you want.

  • Open the *.Context.tt file
  • Go to line ~59
  • Change this code.

    public <#=code.Escape(container)#>()
        : base("name=<#=container.Name#>")
    
  • Into the default constructor you want, for example.

    public <#=code.Escape(container)#>(string nameOrConnectionString)
        : base(nameOrConnectionString)
    
  • Save


You can inherit the DbContext created by the template, define your own constructor, and use the inherited DbContext instead of the one generated by the template.

public class MyModifiedDbContext : TheTemplateGeneratedDbContext
{
   public MyModifiedDbContext() 
   {
       // define your own constructor
   }
}

Or make it private to avoid its use, so you get the error at compile time

public class MyModifiedDbContext : TheTemplateGeneratedDbContext
{
   private MyModifiedDbContext() 
   // ...
}

Use MyModifiedDbContext instead of TheTemplateGeneratedDbContext