0

Lets say I have a class Foods

public class FoodsContext : DbContext, IUnitOfWork
{
   public DbSet<Consumer> Consumers {get; set;}
}

and there is class Fruits

public class FruitsContext: FoodsContext
{
   public DbSet<Price> Prices {get; set;}
}

then in my repository Lets say I have

public class SampleRepository
{
   private readonly FruitsContext _dbFruits = new FruitsContext();

   public void foo()
   {
      _dbFruits.Prices.doanything;
      //how can i use Consumers table that has been set in Foods class
   }
}

In my repository class I want to access values from Consumers table, without creating an instance of Food class. How can I do that?

I had seen this in some project but I don't quite remember it now. Can someone suggest something?

Cybercop
  • 8,475
  • 21
  • 75
  • 135

2 Answers2

2

It looks like plain inheritance to me:

public void foo()
{
  _dbFruits.Prices.doanything;
  //how can i use Consumers table that has been set in Foods class
  _dbFruits.Consumers.doanything;  // this should work
}
H H
  • 263,252
  • 30
  • 330
  • 514
0

You could also do

public void foo()
{
  _dbFruits.Prices.doanything;
  //how can i use Consumers table that has been set in Foods class
  _dbFruits.Set<Consumer>.doanything;
}
Cybercop
  • 8,475
  • 21
  • 75
  • 135
  • Yes, that's a DbContext method. Another base class. – H H Jul 26 '13 at 12:49
  • But do review your design and maybe copy something that was worked out more. And UnitOfWork usually owns 1 or more Repositories, it's not their base class. – H H Jul 26 '13 at 12:50
  • actually my design is much more complex, i just wanted to show the basic random class here. – Cybercop Jul 26 '13 at 13:09