0

I'm working with entity framework and mysql. We created a class

public class DataBaseContext : ObjectContext, IDbContext

There is a method

 public IEnumerable<T> Find<T>(Func<T, bool> whereClause) where T : class
        {
            return CreateObjectSet<T>().Where(whereClause);
        }

Is there a way not to create ObjectSet every time when I call the method? Can I check that it is already exists?

valerii.sverdlik
  • 559
  • 4
  • 18

1 Answers1

0

Whooooo. That is so bad method. You are passing Func<>, not Expression<Func<>>. It means that every time you execute your method EF will pull all records from database table mapped to T and execute your filtering in memory of your application - creating object set is the last thing you should be afraid of.

Anyway creating object set should not be expensive operation and if you don't want to create it every time you need to implement some "local caching" inside your object context instance.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • The reason why I was trying to do that is not a perfomance. I have a "There is already an open DataReader associated with this Connection which must be closed first." exception and trying somehow overcome this problem – valerii.sverdlik Sep 23 '11 at 09:04
  • That problem is absolutely unrelated with object set. You are either using your context instance for concurrent operations (multiple threads) or you have problem mentioned [here](http://stackoverflow.com/questions/4867602/entity-framework-there-is-already-an-open-datareader-associated-with-this-comman/4868569#4868569). – Ladislav Mrnka Sep 23 '11 at 09:14
  • thanks for your answer. I alredy saw that post yesterday. Now I'm trying to figure out how can that help me – valerii.sverdlik Sep 23 '11 at 09:28