c# DbSet - Internal object cannot be got(c# DbSet - 无法获取内部对象)
问题描述
我需要将实体切换到内部.所以我创造了它.没有构建/运行时错误.但是当我想使用 DbSet 对象时,我不能,因为该对象似乎没有初始化!
I need to switch an entity to internal. So I create it. No build/runtime error. But when I want to use the DbSet object I can't because the object seems not initialized !
我的上下文实体:
public partial class Entities
{
internal DbSet<Employee> EmployeeSet { get; set; }
}
我是这样使用的:
Entities context = new Entities();
List<Employee> employees = context.EmployeeSet.ToList();
但EmployeeSet"为空.我认为这是因为它没有在 get 中实例化.如果我像这样使用 public ,它会起作用:
But "EmployeeSet" is null. I think it's because it is not instantiated in get. It works if I use public like this:
public partial class Entities
{
public DbSet<Employee> EmployeeSet { get; set; }
}
问题:如果 DbSet 标记为内部,它可以工作吗?如果有怎么办?为什么会破坏它?(感谢斯科特斯塔福德)
Questions: Can it work if a DbSet is marked internal? If so how? Why does that break it? (thanks Scott Stafford)
推荐答案
不设置为public
不会自动实例化.您可以使用 Set< 手动实例化它.TEntity>()
方法.
It will not be automatically instantiated if it is not set to public
. You can manually instantiate it using Set<TEntity>()
method.
public partial class Entities
{
internal DbSet<Employee> EmployeeSet { get; set; }
public Entities()
{
EmployeeSet = Set<Employee>();
}
}
这篇关于c# DbSet - 无法获取内部对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c# DbSet - 无法获取内部对象


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01