Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 827
Rasmita Dash Avatar asked Jan 16 '15 11:01

Rasmita Dash


People also ask

Which approach will create Entity Framework from an existing database?

The Database First Approach creates the entity framework from an existing database.

What is database first approach in Entity Framework?

Database First allows you to reverse engineer a model from an existing database. The model is stored in an EDMX file (. edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that you interact with in your application are automatically generated from the EDMX file.

How does Entity Framework store data?

Insert Data Add methods add a new entity to a context (instance of DbContext) which will insert a new record in the database when you call the SaveChanges() method. In the above example, context. Students. Add(std) adds a newly created instance of the Student entity to a context with Added EntityState.

What are the three approaches in Entity Framework?

There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.


3 Answers

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.

like image 141
mattpm Avatar answered Sep 27 '22 23:09

mattpm


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

like image 32
dariogriffo Avatar answered Sep 27 '22 22:09

dariogriffo


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>
like image 35
Michael Edenfield Avatar answered Sep 27 '22 23:09

Michael Edenfield