title: Abstracting ef core 2 dbContext
body: <p>I'm trying to make an abstraction over my DB Context layer <strong>(EntityFramework 2.0).</strong></p>
  
  <pre><code>Car.DataContext
   ------------------- 
   public abstract class BaseCarContext : DbContext
      {
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Car&gt;(e =&gt;
              {
                  e.ToTable("Car");             
              });
              modelBuilder.Entity&lt;Car&gt;(e =&gt; { e.ToTable("Cars"); });
          }
      }
  
       public class CarContext : BaseCarContext
      {
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              if (optionsBuilder.IsConfigured)
                  return;
  
              optionsBuilder.UseSqlServer(@"Server = xxxx; Database = xxxx; Trusted_Connection = True;");
          }
  
          public DbSet&lt;Car&gt; Cars { get; set; }        
      }
  
   Car.Logic
   ----------------
   public interface ICarService
   {
      GetCarResponse RetrieveCar(int id);
      void Save(int id);
      ...
   }
  
   public class CarService : ICarService
   {
      private readonly ICarService service;   
      // dbContext interface
  
      public CarService(ICarService service){
          this.service = service; 
          // injecting db context interface
      }
  
      public void Save(int id){
          ... saving using injected db context
          // injected db context.Insert(new Car{ Name = "Honda" });
      }
      ...
   }
  </code></pre>
  
  <p>How can I abstract this ef core 2 <code>CarContext</code> in order to use <code>dbContext</code> save </p>
  
  <p>I tried to make an interface <code>IDbContext</code> which is implemented by <code>CarContext</code> 
   but that way I cannot use <code>dbContext.Cars.Insert</code> because I'm not implementing dbContext cars collection don't have access to ef core methods and properties.</p>
  
  <p>I can use of course concrete implementation but I'm trying to make an abstraction so I can use unit tests, ...</p>
  
  <p>How would you do this?</p>