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

查看:187
本文介绍了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 !

我的ContextEntities:

My ContextEntities:

public partial class Entities
{
   internal DbSet<Employee> EmployeeSet { get; set; }
}

我使用如下:

Entities context = new Entities();
List<Employee> employees = context.EmployeeSet.ToList();

但是EmployeeSet为空。我认为这是因为它没有被实例化。如果我使用这样的公开方式,它会起作用:

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被标记为内部?如果是这样的话为什么会这样呢?
(谢谢Scott Stafford)

Questions: Can it work if a DbSet is marked internal? If so how? Why does that break it? (thanks Scott Stafford)

推荐答案

如果未设置为<$ c $,则不会自动实例化C>公共。您可以使用 设置< 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天全站免登陆