EF 4.1 Code First Configure Database

brotherbill999
brotherbill999 used Ask the Experts™
on
How do I configure an EF 4.1 Code First Database, built on the fly, so that its name is 'MyDatabase', for example.
I don't like the default database name.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
In http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx, comments area:

wjn161
7 Apr 2011 1:10 AM

@ Martin Erskine

the DbContext has some override versions ,you can pass a connectionString like that

public CategoryContext(): base(connectionString){}

>>> This provides the answer.

Author

Commented:
Thank you.  It is just one line of code, but it is a CRITICAL line of code! ;-)

Author

Commented:
Some tips on creating the web.config entry.

0. Database already exists
1. Add WebForm page: WebForm1.aspx
2. Drag SqlDataSource to Design surface
     a. Using smart tag, Configure Data Source...
     b. Choose Your Data Connection wizard step
            Which data connection? Click: New Connection...
              Add Connection dialog
             Server name: MY-PC\SqlExpress
        Connect to a database
                  (o) Select or enter a database name: MyDataBaseName
              Click: Test Connection
        Click: OK
      c. Grab Connection String:
         Data Source=MY-PC\SqlExpress;Initial Catalog=MyDataBaseName;Integrated Security=True
      d. Click Next
      e. [x] Yes, save this connection as MyDataBaseNameConnectionString
      f. Click: Next until Click Finish

If you have used C:\Windows\Microsoft.NET\Framework\v2.0.50727> aspnet_regsql.exe to create the database, change connection string "ApplicationServices" to point to the same config as MyDataBaseName.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial