Should I create explicit DbSet properties in my DbContext?

I am using Entity Framework 6 Code First with an empty database. I created a fairly large number of POCO classes with a rather complex class hierarchy (quite a lot of abstract classes and quite a few concrete classes). I would like to somehow decorate my classes so that they are automatically added to the DbContext without the need to explicitly create a DbSet property for everyone, but I'm worried that this will cause problems when trying to update the database, I saw a couple of threads here, where someone seemed to ask a similar question, but the answer seemed more suited to using DbContext.Set () to get a link to an existing set.

+4
source share
2 answers

I found that I do not need to add explicit DbSet properties to the DbContext for each class, because EF automatically adds all related classes to the model.

I can be lazy and add only explicit properties for some classes, and then refer to any other classes using the DbContext.Set <> () method:

var q = from x in myContext.Set<myClass>() select x;
+1
source

If you rely on primary code migrations, then yes, EF uses reflection on yours DbContextto find out which tables to create. Each property is DbSetmapped to a table in your database.

+2
source

All Articles