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

4

9

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

Tetryl answered 16/1, 2015 at 11:34 Comment(2)
Can you show the app.config?Snodgrass
Added App.config & Context class as wellTetryl
S
5

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

Snodgrass answered 16/1, 2015 at 12:32 Comment(0)
S
11

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.

Spratt answered 5/4, 2016 at 11:4 Comment(1)
"with the name as per your project". You're right for the path, but the name seems to be <namespace>.<name> of the type derived from DbContext used to access the database (at least for (localdb)\MSSQLLocalDB). So in the OP case, the path would be: C:\Users\[yourusername]\DataLayer.Context.mdf (or better using the environment variable for generalization: %HomePath%\DataLayer.Context.mdf`)Biofeedback
S
5

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

Snodgrass answered 16/1, 2015 at 12:32 Comment(0)
P
2

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>
Paraboloid answered 16/1, 2015 at 12:33 Comment(0)
M
0

In ssms type server name (localDB)\.

Multiplicate answered 5/1, 2023 at 20:3 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Showery

© 2022 - 2024 — McMap. All rights reserved.