Cannot find my Entity Framework database

谁都会走 提交于 2020-01-11 06:55:10

问题


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>

回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/29851749/cannot-find-my-entity-framework-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!