Reputation: 937
I am learning Entity Framework. I created a demo app using Entity framework code first approach (without specifying any connection string).
static void Main(string[] args)
{
CreateBlog();
}
private static void CreateBlog()
{
var ObjBlog = new Blog
{
BloggerName = "Rasmita",
Title = "EntityFramework"
};
var Ctx = new Context();
Ctx.Blogs.Add(ObjBlog);
Ctx.SaveChanges();
}
The console App is running fine, It created data base, I am able to fetch data from it. But, I am unable to see it physically. I mean I want to know where it got created? in Local Sql server or Visual studio local db???
App.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Context class
using System.Data.Entity;
using BlogsApp;
namespace DataLayer
{
public class Context:DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
}
I am using EntityFramework 6.1.1, .Net Framework 4.5, Visual studio 2012. I have SqlServer 2012 installed on my machine. Please help me in finding the Db
Upvotes: 9
Views: 13157
Reputation: 1390
In the absence of specifying a connection string and having a default SQLServerExpress instance, the EntityFramework creates sql database files in your user folder (e.g. C:\Users[yourusername]) with the name as per your project.
This files do not appear to be tied to SQL Server i.e. you can freely move them.
Upvotes: 11
Reputation: 28338
According to your configuration file, your default connection factory is:
System.Data.Entity.Infrastructure.SqlConnectionFactory
which is the connection factory for SQL Server. Since you aren't providing a connection string, the Entity Framework is going to try to connect to an instance of SQL Server called .\SQLEXPRESS
, and create a database with the full type name of your context, DataLayer.Context
.
You can change both of those defaults by changing the default connection factory and calling a special overload of the DbContext
constructor, but generally no one bothers.
What you should almost always do instead is to add a connection string named after you context:
<connectionStrings>
<add name="Context"
providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Blogging;Integrated Security=True;MultipleActiveRecordSets=True"/>
</connectionStrings>
or
<connectionStrings>
<add name="Context"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Blogging;Integrated Security=True;MultipleActiveRecordSets=True"/>
</connectionStrings>
Upvotes: 2
Reputation: 4274
It is in your SQLEXPRESS instance. Probably will be called ConsoleApplication1.Context. If you have sql server studio manager use .\SQLEXPRESS as the server name, and you will see there the DB. You can get the file location from there, but should be located at
C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data
or
C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data
if you are running 32 bits windows.
From here
"If SQL Express is installed (included in Visual Studio 2010) then the database is created on your local SQL Express instance (.\SQLEXPRESS). If SQL Express is not installed then Code First will try and use LocalDb ((localdb)\v11.0) - LocalDb is included with Visual Studio 2012"
From here
http://msdn.microsoft.com/en-us/data/jj591621.aspx
Upvotes: 5