Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# DbSet - Internal object cannot be got

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 !

My ContextEntities:

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

I use like this:

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

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; }
}

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

like image 675
Sam Avatar asked Oct 03 '11 14:10

Sam


2 Answers

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>();
   }
}
like image 171
Eranga Avatar answered Sep 29 '22 11:09

Eranga


I just had the same problem and was able to fix it by only setting the getter as internal.

public partial class Entities
{
   public DbSet<Employee> EmployeeSet { internal get; set; }
}
like image 35
Arne Avatar answered Sep 29 '22 12:09

Arne