Normally the Dbcontext
that represents your database has several DbSet<TEntity>
properties, where every DbSet
represents a table in your database.
All non-virtual properties of TEntity
represent the columns in your table; the virtual properties of TEntity
represent the relations between tables: one-to-many, many-to-many etc.
Every DbSet<TEntity>
implements IDbSet<TEntity>
so wherever your old DbContext
used an IDbSet
, you can give it the corresponding DbSet
instead.
If I understand you correctly, your old DbContext
had some properties that implemented IDbSet<...>
, and method SomeRandomMethod
used these properties.
class MyOldDbContext : DbContext
{
public IDbSet<Aentity> Aentities {get; set;}
public IDbSet<Bentity> BEntities {get; set;}
public void SomeRandomMethod()
{ // this method uses the IdBsets AEntities and BEntities:
IDbSet<AEntity> x = this.AEntities;
IDbSet<BEntity> y = this.BEntities;
... // do something with x and y
}
}
Now your new DbContext. If the DbSets have the same Entity types as the old ones, there is no problem:
class MyNewDbContext : DbContext
{
public DbSet<Aentity> Aentities {get; set;}
public DbSet<Bentity> BEntities {get; set;}
public void SomeRandomMethod()
{ // this method uses the DbSets AEntities and BEntities, which implement IDbSet
IDbSet<AEntity> x = this.AEntities;
IDbSet<BEntity> y = this.BEntities;
... // do something with x and y
}
}
Note that AEntities/BEntities are now DbSet<...>
instead of IDbSet<...>
. Because every DbSet<...>
implements IDbSet<...>
, your problem is solved.
It is a bit more difficult if your new tables are different than your old ones. In that case you'll have to write adapter properties, that return the expected IDbSet<...>
class MyNewDbContext : DbContext
{
public DbSet<Teacher> Teachers {get; set;}
public DbSet<Student> Students {get; set;}
public void SomeRandomMethod()
{ // this method uses the IdBset of AEntities and BEntities:
IDbSet<AEntity> x = this.AEntities;
IDbSet<BEntity> y = this.BEntities;
... // do something with x and y
}
// for SomeRandomMethod we need properties AEntities and BEntities
// use your new DbSets to mimic the old AEntities and BEntities
private IDbSet<AEntity> AEntities => this.Teachers
.Join(this.Students, ...)
.Where(...)
.Select(...);
// similar for BEntities
}