Entity Framework 4 Code First: Where is my database?

假如想象 提交于 2019-12-01 22:34:22
SAm
C:\Users\YourCurrentUserName  

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

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!

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.

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!