Search code examples
c#entity-frameworkdatacontext

Default physical location of Data base created in Entity Framework codefirst approach


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


Solution

  • 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