Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an in-memory provider for Entity Framework?

I am unit testing code written against the ADO .NET Entity Framework. I would like to populate an in-memory database with rows, and make sure that my code retrieves them properly.

I can mock the Entity Framework using Rhino Mocks, but that would not be sufficient. I would be telling the query what entities to return to me. This would neither test the where clause nor the .Include() statements. I want to be sure that my where clause matches only the rows I intend, and no others. I want to be sure that I have asked for the entities that I need, and none that I don't.

For example:

class CustomerService {     ObjectQuery<Customer> _customerSource;     public CustomerService(ObjectQuery<Customer> customerSource)     {         _customerSource = customerSource;     }     public Customer GetCustomerById(int customerId)     {         var customers = from c in _customerSource.Include("Order")             where c.CustomerID == customerId             select c;         return customers.FirstOrDefault();     } } 

If I mock the ObjectQuery to return a known customer populated with orders, how do I know that CustomerService has the right where clause and Include? I would rather insert some customer rows and some order rows, then assert that the right customer was selected and the orders are populated.

like image 621
Michael L Perry Avatar asked Feb 22 '09 16:02

Michael L Perry


People also ask

What is Entity Framework in memory?

Entity Framework Core InMemory InMemory is an in-memory database provider for Entity Framework Core. It is useful when you want to test components using something that approximates connecting to the real database, without the overhead of actual database operations.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Does Entity Framework core cache?

Using Caching in Entity Framework Core. NCache provides integration for caching in Entity Framework Core through Extension Methods. You can cache the result sets of LINQ queries whether they are for transactional data or reference data.

Is hibernate like Entity Framework?

Hibernate is a suite of open source projects around domain models. The flagship project is Hibernate ORM, the Object Relational Mapper; Entity Framework Core: *Lightweight and cross-platform version of the popular Entity Framework *.


2 Answers

An InMemory provider is included in EF7 (pre-release).

You can use either the NuGet package, or read about it in the EF repo on GitHub (view source).

like image 52
Shimmy Weitzhandler Avatar answered Sep 23 '22 07:09

Shimmy Weitzhandler


The article http://www.codeproject.com/Articles/460175/Two-strategies-for-testing-Entity-Framework-Effort  describes Effort  -Entity Framework provider that runs in memory.

You can still use your DbContext or ObjectContext classes within unit tests, without having to have an actual database.

like image 29
Michael Freidgeim Avatar answered Sep 23 '22 07:09

Michael Freidgeim