This project has moved and is read-only. For the latest updates, please go here.

In this tutorial we will create a fake ObjectContext instance.

The code below shows a basic approach of querying data with Entity Framework.

public IList<Product> GetAllProducts()
    using (NorthwindEntities ctx = new NorthwindEntities())
        return ctx.Products.ToList();

Behind the scenes a connection is established with the appropriate database server and an SQL command is executed. Effort makes it possible to eliminate these communication completely and run every required operation by the current process. All we need to do is a simple modification:

public IList<Product> GetAllProducts()
    using (NorthwindEntities ctx = 
Effort.ObjectContextFactory.CreateTransient<NorthwindEntities>()) { return ctx.Products.ToList(); } }

The ObjectContextFactory class provides helper methods that can create ObjectContext instances that behave differently: if you execute the code above, there will be no communication with your external database server, instead the data operations will be executed by an in-process lightweight database.

This fake database is empty at this point, so the returned collection will be always empty. To overcome this issue, Effort provides a simple way to initialize your fake database from differnent sources (e.g. CSV files). You can find more information in another tutorial.

Last edited Nov 10 at 6:17 AM by zzzprojects, version 15


jorgegarmilla Feb 7, 2015 at 8:52 PM 
jdege .... see here:

tamasflamich Dec 28, 2013 at 2:15 AM 
The NothwindEntities class is derived from ObjectContext. Do you know about the differences between DbContext and ObjectContext?

jdege Dec 27, 2013 at 10:29 PM 
From your example, I'd assume that NorthwindEntities is the EntityFramework DbContext class - derived from System.Data.Entity.DbContext. Which would mean that to construct a fake ObjectContext for my app, I'd need to pass my DbContext class to CreateTransient. But when I do, I get an error:

There is no implicit reference conversion from 'MyDbContext' to 'System.Data.Entity.Core.Objects.ObjectContext'.

Was the example above written for an older version of EF? I'm using EF 6.0.2.