Given this kind of context:
public class FooContext : DbContext
{
public FooContext(DbContextOptions<FooContext> opts) : base(opts)
{ }
public DbSet<Bar> Bars { get; set; }
}
I can get to a Bar
in two ways:
fooContext.Bars.Add(new Bar()); // Approach 1
or
fooContext.Set<Bar>().Add(new Bar()); // Approach 2
What is the difference between the two approaches?
I've tried to answer my own question by:
- Inspecting the intellisense for both (only tells me that
Set<T>()
also creates aDbSet<T>
) - Googling for "EF Core Set vs property" but that doesn't seem to be the 'right' query
- Google for
DbSet<T>
specifically on the docs urls but no relevant results here either it seems - Reading the intro of the
DbSet<T>
docs which just suggests that you can get a set through either of the two methods (not if there is or isn't a difference) - Read the
Set<T>()
docs which has no relevant info
But I could not find any good explanation about which of the two is used for which purpose. What is the difference? Or perhaps more importantly: where and how should I be able to find this in the docs?