Entity Framework 7 (EF core 1.0) In-Memory Provider (for testing) simplified

Entity Framework is well-known Microsoft open source (from EF 5) data access technology for .Net applications. Rowan Miller announced their team plans and future of Entity framework 7 in TechEd America 2014. You can view recorded video on Channel 9.
ef-7-in-memory-data-store-for-testingEntity Framework 7 (EF7) provides a familiar developer experience to previous versions of EF, including LINQ, POCO, and Code First support. EF7 also enables access to data across relational and non-relational stores. EF7 is much more lightweight than previous versions and is built from the ground up to work great in the cloud (using ASP.NET vNext) on devices (i.e. in universal Windows apps) as well as in traditional .NET scenarios.

Entity Framework is currently in pre-release and enhancing with new features in every release. In this article I am going to share one of useful feature "In-memory store" using Console Application. 

To test Entity Framework DB operations, we need to mock out Dbcontext which is not an easy task. In-memory store is useful for testing your data context with out mock out it. In-memory store will behave same as our actual data store.Using In-Memory data store you can experience same as real data store without setup. 

Install Entity Framework in project, Entity Framework is extensible now. so, you have to choose and install required extension rather than a whole Entity framework. Here we need to install EntityFramework.core and In-memory provider only.
Install-Package EntityFramework.Core -Pre
Install-Package EntityFramework.InMemory -Pre
If you had installed only EntityFramework.Core and trying to perform any Database operation, it will throw Data provider not found error.

Add Employee class

Start with adding Employee class as our POCO.
public class Employee
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }

Add context class and configure it

Next we will define our data context class. Microsoft EF makes too many changes in the data context (that we will discuss later) but Registering POCOs in DbContext is as same as the previous version.
public class DataContext : DbContext
    public DbSet<Employee> Employee { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
To configure In-memory as your data store, you have to override OnConfiguring method. This method helps to build your DbContext options. I have added options.UseInMemoryDatabase() it will consider it as I want to use an In-memory provider as database. Add DbContext class as we are doing in previous version of Entity Framework.

Wrap up all code

It's time to wrap up all code using DataContext. We have already configured In-memory as our Data provider so we do not need to do anything else. Add and retrieve Employee objects same way as we are doing. It will store in our memory instead of the real database.
class Program
    static void Main(string[] args)
        using (var dataContext = new DataContext())
            dataContext.Employee.Add(new Employee() { BirthDate = DateTime.Now, FirstName = "Bhavik", LastName = "Patel" });
            dataContext.Employee.Add(new Employee() { BirthDate = DateTime.Now, FirstName = "Sohil", LastName = "Patel" });

            foreach (var employee in dataContext.Employee.ToList())
                Console.WriteLine($"Id= {employee.Id}, FirstName= {employee.FirstName}, LastName= {employee.LastName} and DateofBirth= {employee.BirthDate}");



ef7-in-memory-providerAs, It will store two employees and retrieve from same. You can perform CRUD operations on the same running instance.
Download source code at Github

In-memory provider specially used for unit testing application without mock out required application classes.


Microsoft recently renamed Entity Framework 7 to Entity Framework Core 1.0

Good References


Popular posts from this blog

12 opensource and free asp.net MVC gridview contols

Scheduling background jobs using Hangfire in ASP.NET Core

Difference ExecuteNonQuery vs ExecuteScalar vs ExecuteReader in asp.net