Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change EF 4.1 Code First Default DB Location

I'm working through the Building an MVC 3 App with Code First and Entity Framework 4.1 tutorial on MSDN and got stuck on "Also by default, this database will be a SQL Express database with the name derived from the strongly typed name of the context and its file will be in the SQL Express default data folder."

If I want to change the default (e.g. to place the MDF file in my App_Data folder) how would I do that? I will have several different contexts (one for each major functional area) and would like them all to live in the same database.

like image 821
Eric J. Avatar asked Apr 12 '11 02:04

Eric J.


People also ask

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

What is Entity Framework DB first?

Database First allows you to reverse engineer a model from an existing database. The model is stored in an EDMX file (. edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that you interact with in your application are automatically generated from the EDMX file.

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add.


1 Answers

You define where the database lives using the web.config file connection settings. You just have to make the Context Name = your connection string name so if the of you Context is MyContext you could define the location as below:

    <connectionStrings>
    <clear/>
        <add name="MyContext" 
         connectionString="Server=myServer;Database=MyDB;Uid=foo;Password=XXX; " 
         providerName="System.Data.SqlClient"
           />


  </connectionStrings>
like image 96
Daveo Avatar answered Oct 04 '22 17:10

Daveo