Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 568
gnychis Avatar asked Mar 07 '26 14:03

gnychis


2 Answers

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

like image 138
Praveen Paulose Avatar answered Mar 10 '26 04:03

Praveen Paulose


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.

like image 40
Paul Richards Avatar answered Mar 10 '26 02:03

Paul Richards