Constructor of Dbcontext keeps changing the base argument on update of model

c# entity-framework entity-framework-6 visual-studio

Question

In my context class I have the following to make the project a black box for data.

     public MyEntities()
        : base("metadata = res://*/fda.csdl|res://*/fda.ssdl|res://*/fda.msl;provider=System.Data.SqlClient;provider connection string='data source=MyServer;initial catalog=CitywideEmployees;persist security info=True;user id=MyUserID;password=MyPassword!;MultipleActiveResultSets=True;App=EntityFramework'")
    {
    }

However when I update the edmx, using the "Update Model from Database" option, it always resets the base argument, almost like it wants to look in an App.config file for the connection string. How do I prevent having to update the base every time I update the edmx?

Thanks.

1
0
9/28/2018 12:48:06 PM

Accepted Answer

Open your {your name}.Context.tt and save the following change

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
{
    public <#=code.Escape(container)#>()
        : base("name=<#=container.Name#>")
    {
<#

In quotes where it says "name=<#=container.Name#>"

replace it with

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
{
    public <#=code.Escape(container)#>()
        : base("metadata = res://*/fda.csdl|res://*/fda.ssdl|res://*/fda.msl;provider=System.Data.SqlClient;provider connection string='data source=MyServer;initial catalog=CitywideEmployees;persist security info=True;user id=MyUserID;password=MyPassword!;MultipleActiveResultSets=True;App=EntityFramework'")
    {
<#

save your tt file

1
9/30/2018 6:35:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow