gnychis
gnychis

Reputation: 7555

Cannot find my Entity Framework database

I am a little confused on the code first entity framework database.

I created a new DbContext and class that I will be storing in that context, like this:

namespace MyProject.Subproject.Something
{
    public class MyItem
    {
        public string One { get; set; }
        public string Two { get; set; }
        [Key]
        public string Three { get; set; }
        public string Four { get; set; }
    }

    public class MyAppData : DbContext
    {
        public DbSet<MyItem> MyItems { get; set; }
    }
}

I know that it's working since I can do this without failure:

var db = new MyAppData();
MyItem i = new MyItem();
// ... fill in item
db.MyItems.Add(i);
db.SaveChanges();

Additionally, if I restart the application I find db.MyItems.Count() to reflect that items are in fact persistently stored somewhere.

I'm assuming this is stored in localdb since I set up no SQL Server database. All I want to be able to do is see the table in localdb somewhere, however I can't seem to find it.

If I go to Data Sources -> Add New Data Source -> Database -> Data Set -> New Connection -> Microsoft SQL Server and then put in (localdb)v11.0 for the Server Name and dropdown the list of databases, I do not see MyAppData or MyItems listed.

Edit: What I see in my App.config is <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">


Edit 2: Full 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=xxxxxxx" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Upvotes: 2

Views: 4847

Answers (3)

Paul Richards
Paul Richards

Reputation: 1207

using (var db = new BloggingContext()) 
{ 
  var connectionString = db.Database.Connection.ConnectionString;
  Console.WriteLine(connectionString); 
}

The above code uses the Microsoft example; for BloggingContext substitute your DbContext class. It will give you the connection string that the entity framework is using, which includes the instance name.

Upvotes: 2

Praveen Paulose
Praveen Paulose

Reputation: 5771

You need to set your database server as (LocalDB)\MSSQLLocalDB. This is a change made to EF 6.1.1 onwards. The MSSQLLocalDB is mentioned in your parameter. More details on the change can be found here https://entityframework.codeplex.com/workitem/2246

MSSQLLocalDB is the default instance name on SQL Server 2014 whereas v11.0 is the default Instance name on SQL Server 2012

Upvotes: 10

voidzero
voidzero

Reputation: 594

Yep - based on the parameter in your defaultConnectionFactory it looks like you're using the default SQL 2014 instance, not the 2012 instance.

The (localdb)\ProjectsV12 instance is created by SQL Server Data Tools (SSDT)

(localdb)\MSSQLLocalDB is the SQL Server 2014 LocalDB default instance name

And (localdb)\v11.0 is the SQL Server 2012 LocalDB default instance name

Upvotes: 0

Related Questions