Ben
Ben

Reputation: 5715

Entity Framework 4 Code First: Where is my database?

Creating a web site using code first, for my own piece of mind i would like to be able to explore the db it creates like you can when you use database first and create the SQL server db within VS2010.

I have had a look around, and the only related information i can find seems to be using a previously existing db with code first, I want the db to be made by code first and i just want to know where it is, or how to change the default location of it so i can look in it.

I have had a fiddle with the connection strings in the config file, but don't really know what i am doing so havent had any success there. It worked fine with a custon connection string, but i still don't know where the db file is!

Upvotes: 3

Views: 6464

Answers (7)

Bondolin
Bondolin

Reputation: 3121

Like @SAm has said, you can search for your database file by the name of the context class that forms it, and in certain default cases this will land you the file in your user directory. I was having trouble actually connecting to this database in VS Server Explorer, because I didn't know the connection name. So instead I added the database file by Data Connections --> Add Connection... --> Change Data Source --> Microsoft SQL Server Database File. This prompted me to browse for the database file which we've located, that gave me a connection, and that in turn gave me a connection string which viola! indicates the database is also stored at (LocalDB)\MSSQLLocalDB. Found a good many other databases I've messed around with there.

Upvotes: 0

rakeshyadvanshi
rakeshyadvanshi

Reputation: 293

hi @kingDango this will give the connection string and server name..

 Console.WriteLine(ac.Database.Connection.ConnectionString);

use this server name in sql server management studio and paste in server filed and connect.. you can find the database..

or second case it may in App_Data

Upvotes: 0

SAm
SAm

Reputation: 2232

C:\Users\YourCurrentUserName  

I found it in the above directory.
By doing a windows search on the Context class name.

Upvotes: 9

kingdango
kingdango

Reputation: 3999

EF Code First has multiple ways of defining where and how the database gets created. In fact it has a number of Conventions (in lieu of specific configuration or code). I prefer to be specific (see my connection string example below) but if you use the convention it is defined by Microsoft as below:

Here's a snippet from that page:

Connection String Convention

The Entity Framework uses the default conventions to create the database on the localhost\SQLEXPRESS instance or LocalDB server, and names the database after the fully qualified type name of the derived context (for example, CodeFirstModel.SchoolEntities).

Visual Studio 11 includes LocalDb database server rather than SQLEXPRESS. During installation, the EntityFramework NuGet package checks which database server is available. The NuGet package will then update the configuration file by setting the default database server that Code First uses when creating a connection by convention. If SQLEXPRESS is running, it will be used. If SQLEXPRESS is not available then LocalDb will be registered as the default instead. No changes are made to the configuration file if it already contains a setting for the default connection factory. If the connection string is set in code, then the instance set in code will take precedence over anything found in the config file.

When the application is run subsequent times, unless the model changes, the existing database will be used. If the model changes and you do not set the initializer, you will get the exception: “The model backing the 'ContextName’ context has changed since the database was created. Consider using Code First Migrations to update the database.”

One way to override the Code First convention for the database name is to add an App.config or Web.config file that contains the connection string with the same name as your context type. The .config file should be added to the project that contains the executable assembly.

Note: When working with Code First, your connection string should be a regular database connection string. When working with the Entity Framework Designer,, the connection string should be an EntityConnection string.

Below is an example of one of my EF Code First connection strings:

<add name="NameOfYourDbContextClass" connectionString="Data Source=YOUR-DB-SERVER;Initial Catalog=THE-DB-NAME-YOU-WANT;Persist Security Info=True;Trusted_Connection=true;" providerName="System.Data.SqlClient" />

One last thing to keep in mind... connection strings (and configuration in general) are defined by the application context. In other words, if you have your Data Access class (based on DbContext) in another project the connection strings still need to be defined in your web.config (in the web project) as an example.

Good luck Ben!

Upvotes: 8

apros
apros

Reputation: 2878

Suppose you create your POCO class like YourContext : DbContext 1. If you defined connection string with name=YourContext than you connect to specific database. 2. In other case your database will be created on local SQLEXPRESS instance with name YOUR_NAMESPACE.YourContext

Upvotes: 0

Antarr Byrd
Antarr Byrd

Reputation: 26169

If you are using sql server or sql server express you will need Sql Server Management Studio

If you are using the compact version then there should be a file under app_data in your solution. You should be able to determine this by looking at your Web.config file.

Upvotes: 0

Justin Pihony
Justin Pihony

Reputation: 67135

Scott Gu wrote a good article on code first. You can search for connection if you want to go straight to the part about connection strings, then generate will explain the generation process.

Upvotes: 0

Related Questions