我创建了一个新的 Entity Framework Code First 应用程序,DbSet (People) 返回 null。
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Repository : DbContext
{
public DbSet<Person> People;
}
web.config:连接字符串
<connectionStrings>
<add name="Repository"
connectionString="Data Source=|DataDirectory|Repository.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
现在当我打电话
Repository _repo = new Repository()
_repo.People;
_repo.People
将为 null
我缺少什么?
- Microsoft.Data.Entity.Ctp.dll 是 引用
- 我尝试过和 没有数据库初始化程序。
最佳答案
那是因为您定义了 DbSet<Person>
的字段在存储库类而不是属性上。添加属性或将其更改为自动属性后,People
将开始为您提供值而不是 null。因此,您需要做的就是将 Repository 类更改为:
public class Repository : DbContext
{
public DbSet<Person> People { get; set; }
}
关于entity-framework - 为什么我的 DbContext DbSet 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4270794/