【问题标题】:Cannot find my Entity Framework database找不到我的实体框架数据库
【发布时间】:2015-04-30 01:53:55
【问题描述】:

我对代码优先实体框架数据库有点困惑。

我创建了一个新的 DbContext 和我将存储在该上下文中的类,如下所示:

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; }
    }
}

我知道它可以正常工作,因为我可以做到这一点而不会失败:

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

此外,如果我重新启动应用程序,我会发现 db.MyItems.Count() 以反映项目实际上永久存储在某处。

我假设它存储在localdb 中,因为我没有设置 SQL Server 数据库。我想做的就是在某个地方看到localdb 中的表格,但我似乎找不到它。

如果我转到Data Sources -&gt; Add New Data Source -&gt; Database -&gt; Data Set -&gt; New Connection -&gt; Microsoft SQL Server,然后输入(localdb)v11.0 作为服务器名称并下拉数据库列表,我看不到 MyAppData 或 MyItems 列出。

编辑:我在 App.config 中看到的是&lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"&gt;


编辑 2:完整的 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>

【问题讨论】:

  • 您的配置中必须有一个连接字符串。检查那里,它会显示路径。
  • 如果localdb中没有,试试SQL Express。使用[My-PC-NAME]\SQLEXPRESS 作为连接字符串。对我来说,它是 JEROEN-DESKTOP\SQLEXPRESS。请注意,名称可能是“DefaultConnection”或类似名称。
  • @PraveenPaulose 这就是我所看到的:&lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"&gt;
  • 您能否将配置中的完整连接节点粘贴到帖子中
  • @PraveenPaulose 添加了完整的 App.config

标签: c# .net entity-framework


【解决方案1】:

您需要将数据库服务器设置为(LocalDB)\MSSQLLocalDB。这是对 EF 6.1.1 及更高版本所做的更改。您的参数中提到了 MSSQLLocalDB。有关更改的更多详细信息,请点击此处https://entityframework.codeplex.com/workitem/2246

MSSQLLocalDB 是 SQL Server 2014 上的默认实例名称,而 v11.0 是 SQL Server 2012 上的默认实例名称

【讨论】:

    【解决方案2】:
    using (var db = new BloggingContext()) 
    { 
      var connectionString = db.Database.Connection.ConnectionString;
      Console.WriteLine(connectionString); 
    }
    

    以上代码使用微软示例;对于 BloggingContext 替换您的 DbContext 类。它将为您提供实体框架正在使用的连接字符串,其中包括实例名称。

    【讨论】:

    • 这帮助我解决了一个隐蔽的问题。简单而有效。干杯
    【解决方案3】:

    是的 - 根据您的 defaultConnectionFactory 中的参数,您似乎使用的是默认 SQL 2014 实例,而不是 2012 实例。

    (localdb)\ProjectsV12 实例由 SQL Server Data Tools (SSDT) 创建

    (localdb)\MSSQLLocalDB 是 SQL Server 2014 LocalDB 默认实例名称

    并且 (localdb)\v11.0 是 SQL Server 2012 LocalDB 默认实例名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多