Entity Framework 4 Code First: Where Is My Database?
First create a website using code, for my own mind I would like to explore the db it creates as much as you can when you first use the database and create the SQL server db in VS2010.
I have looked around and the only related information I can find seems to be using a pre-existing db with code first, I want the db to be done by code first and I just want to know where it is or how to change the location default so I can look in it.
I had a fiddle with connection strings in a config file, but I don't really know what I'm doing, so I didn't have any success. It worked fine with the custon connection string, but I still don't know where the db file is!
source to share
EF Code First has several ways to determine where and how a database is created. In fact, it has a number of Conventions (instead of a specific configuration or code). I prefer to be specific (see my example connection string below), but if you are using a convention, it is defined by Microsoft as shown below:
Here is a snippet from that page:
String Concatenation Convention
The Entity Framework uses default conventions to create a database on the localhost \ SQLEXPRESS instance or local server and the database name after the fully qualified context type name (for example, CodeFirstModel.SchoolEntities).
Visual Studio 11 includes the LocalDb database server, not SQLEXPRESS. During installation, the EntityFramework NuGet package checks which database server is available. Then the NuGet package will update the config file to set the default database server, this code will first use it when creating a convention connection. If SQLEXPRESS is running, it will be used. If SQLEXPRESS is not available then LocalDb will be registered as default. No change. made in the config file if it already contains a parameter for the default factory connection. If the connection string is set in code, then the instance specified in the code will take precedence over anything found in the config file.
When the application starts with subsequent moments, if only the change model, the existing database will be used. If the model changes and you do not set the initializer, you will receive an exception: "Model ContextName support has changed since the database was created. Consider using First 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 containing a connection string with the same name as your context type. The .config file must be added to the project containing the executable assembly.
Note. When working with First code, your connection string should be a regular database connection string. When working with an entity Framework Designer, the connection string must be EntityConnection String.
Below is an example of one of my first EF Code connection lines:
<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 remember ... connection strings (and configuration in general) are determined by the application context. In other words, if you have your data access class (based on DbContext) in another project, the connection strings should still be defined in your web.config (in the web project) as an example.
Good luck Ben!
source to share
Scott Gu wrote a good article on code. You can search for connection if you want to go straight to the connection strings part, then generate will explain the generation process.
source to share
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 your solution should have a file called app_data. You should be able to determine this by looking at the Web.config file.
source to share
Suppose you have created your POCO class, for example YourContext: DbContext 1. If you defined a connection string named = YourContext than you are connecting to a specific database. 2. Otherwise, your database will be created on a local SQLEXPRESS instance named YOUR_NAMESPACE.YourContext
source to share
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 insert the server registered and connected. you can find the database.
or the second case, it could be in App_Data p>
source to share