gny*_*his 2 .net c# 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; }
}
}
Run Code Online (Sandbox Code Playgroud)
我知道它有效,因为我能做到这一点而不会失败:
var db = new MyAppData();
MyItem i = new MyItem();
// ... fill in item
db.MyItems.Add(i);
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
此外,如果我重新启动应用程序,我发现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>
Run Code Online (Sandbox Code Playgroud)
Pra*_*ose 10
您需要将数据库服务器设置为(LocalDB)\MSSQLLocalDB.这是对EF 6.1.1以后的更改.您的参数中提到了MSSQLLocalDB.有关更改的更多详细信息,请访问https://entityframework.codeplex.com/workitem/2246
MSSQLLocalDB是SQL Server 2014上的默认实例名称,而v11.0是SQL Server 2012上的默认实例名称
| 归档时间: |
|
| 查看次数: |
4528 次 |
| 最近记录: |