我对代码优先 Entity Framework 数据库有点困惑。
我创建了一个新的 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()
以反射(reflect)项目实际上永久存储在某个地方。
我假设它存储在 localdb
中因为我没有设置 SQL Server 数据库。我想要做的就是查看 localdb
中的表格某处,但我似乎找不到它。
如果我去 Data Sources -> Add New Data Source -> Database -> Data Set -> New Connection -> Microsoft SQL Server
然后输入 (localdb)v11.0
对于服务器名称和数据库列表的下拉列表,我没有看到列出的 MyAppData 或 MyItems。
编辑:我在 App.config 中看到的是 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
编辑 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)\MSSQLLocalDB
。这是从 EF 6.1.1 开始所做的更改。您的参数中提到了 MSSQLLocalDB。有关更改的更多详细信息,请参见此处 https://entityframework.codeplex.com/workitem/2246
MSSQLLocalDB 是 SQL Server 2014 上的默认实例名称,而 v11.0 是 SQL Server 2012 上的默认实例名称
关于c# - 找不到我的 Entity Framework 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29851749/