Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-First change data source without connection string

I seem to have an issue creating an MVC4 application where I have adopted the code-first approach to creating my models but no connection string seems to have been created in the web.config file.

The constructed database seems to have been built on (localhost)\SQLEXPRESS instance but I would like to change this to an external data source. Without a connection string to update I'm not sure how to do this.

Please could someone point me in the right direction?

EDIT: I found the following diagram which highlights what the answers have said pretty well

EF DB Initialization Flowchart
(source: entityframeworktutorial.net)

like image 872
Chris Pickford Avatar asked May 12 '26 02:05

Chris Pickford


1 Answers

You have to add a connection string yourself

<add name="SomeDb" connectionString="Data Source=SERVENAME;Initial Catalog=DBNAME;User Id=loginid;Password=password" providerName="System.Data.SqlClient" />

Name of this string should match to the name of your context class.

public class SomeDb: DbContext
{
    public SomeDb()
        : base("name=SomeDb")
    { 
    }
}

This should do it.

like image 77
HBhatia Avatar answered May 13 '26 14:05

HBhatia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!