thenna_ tick thenna_ tick - 2 months ago 17x
C# Question

Entity Framework model first connection string

I am using Entity Framework model-first. After finishing my site, I did a

. I am using Entity Framework and a database connection setup using a connection string from settings.config:

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

I have config changed server database details.

My entity framework connection string in

<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
file with server details I get an error

Cannot open database "tickandtie" requested by the login

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


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.