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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With