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

1 Answer 1

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.

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

3 Comments

Please Explain more about it
thank. I have Sql db connection string appsettings Folder and Entity framework connection string web config file ,where i apply this code ?
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. class

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.