c# DbSet - 无法获取内部对象 [英] c# DbSet - Internal object cannot be got

查看:21
本文介绍了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 - 无法获取内部对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆