1

I am using Entity Framework model-first. After finishing my site, I did a Publish. I am using Entity Framework and a database connection setup using a connection string from settings.config:

<add key="thenna" 
     value="server=11.3.34.45;database=montage;user id=sample;password=Test;trusted_connection=false;"/>

I have config changed server database details.

My entity framework connection string in web.config:

<add name="tickandtieEntities" 
     connectionString="metadata=res://*/Entityframework.Tickmarks.csdl|res://*/Entityframework.Tickmarks.ssdl|res://*/Entityframework.Tickmarks.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-QD6A981\SQLEXPRESS;initial catalog=tickandtie;user id=sa;password=tickmarks;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

When I change web.config file with server details I get an error

Cannot open database "tickandtie" requested by the login

How can I configure Entity Framework in web.config when I move my app to the host server? Please help me anyone

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

You can do this by setting the connection string on your EF Db Context at creation time, passing your setting value to your EF context.

E.g.: adding a constructor on your context, which uses the base DbContext constructor to pass the connection string:

public class MyDbContext : DbContext
{
    public MyDbContext(string connString) : base(connString)
    {
    }
}

Which then make your context used like:

var connectionString = "" // Get the value of of your custom config file here.
var ctx = new MyDbContext(connectionString);

As stated above, you need to read your connection string value first out of your settings.config file.

Community
  • 1
  • 1
Benjamin Soulier
  • 2,223
  • 1
  • 18
  • 30
  • Please Explain more about it – thenna_ tick Aug 04 '16 at 13:31
  • thank. I have Sql db connection string appsettings Folder and Entity framework connection string web config file ,where i apply this code ? – thenna_ tick Aug 04 '16 at 13:44
  • Using the code above means you don't need to use the default EF connection string in Web.config. You can then get it from a specific location (e.g. appSettings) using the [ConfigurationManager](https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx). class – Benjamin Soulier Aug 04 '16 at 13:51