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 - 无法获取内部对象
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
