0

I'm new to C# and EntityFramework and I need to maintain an active software. I needed to create a new table so I created it in the database and then updated my Model with EntityFramework.

Howewer, it looks like the previous developer had been writing code directly in generated code (The Mode.Context.cs class) and EntityFramework when updating the model is wiping it and rewritting it completly.

So I did a new partial class Model. It looks like this :

public partial class Model : DbContext, IModel
    {
        public void SomeRandomMethod();
    }

And the generated Model looks like this :

public partial class Model : DbContext
{
    public Model()
        : base("name=Model")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<RandomTable> ARandomTable { get; set; }
}

The problem, however, is that previously, the model was using IDbSet instead of DbSet and the interface IModel is asking for IDbSet<RandomTable> ARandomTable

What would be the proper way to deal with that ?

bommelding
  • 2,969
  • 9
  • 14
AnonBird
  • 570
  • 13
  • 27
  • Update the code a little, this `IModel` can only be asking for SomeRandomMethod(). Be clear about what is your code, and why you need an I in IDbSet. – bommelding Oct 30 '18 at 10:38
  • 1
    If the generation tool is using T4 then just update the T4 model. – CodeNotFound Oct 30 '18 at 10:39
  • @CodeNotFound Thanks, managed to to it with templates. Just needed a hint to what to look at :) – AnonBird Oct 30 '18 at 10:52
  • Which gneration tool are you using? If T4 templates, then go the file and look at the line (CTRL + F) that set `IDbSet`. Save the file and regenerate the code. – CodeNotFound Oct 30 '18 at 10:54

1 Answers1

1

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
}
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116