【发布时间】: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 -> 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中没有,试试SQL Express。使用
[My-PC-NAME]\SQLEXPRESS作为连接字符串。对我来说,它是 JEROEN-DESKTOP\SQLEXPRESS。请注意,名称可能是“DefaultConnection”或类似名称。 -
@PraveenPaulose 这就是我所看到的:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> -
您能否将配置中的完整连接节点粘贴到帖子中
-
@PraveenPaulose 添加了完整的 App.config
标签: c# .net entity-framework