A Quick Look At Entity Framework 7 In-Memory Provider (for Testing)

Entity Framework is A 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.

Testing

Entity 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 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 without 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 -Pr

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.

  1. public class Employee  
  2. {  
  3.     [Key]  
  4.     public int Id  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     public string FirstName  
  10.     {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string LastName  
  15.     {  
  16.         get;  
  17.         set;  
  18.     }  
  19.     public DateTime BirthDate  
  20.     {  
  21.         get;  
  22.         set;  
  23.     }  
  24. }  
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 same as in the previous version.
  1. public class DataContext: DbContext  
  2. {  
  3.     public DbSet < Employee > Employee  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     protected override void OnConfiguring(DbContextOptionsBuilder options)  
  9.     {  
  10.         options.UseInMemoryDatabase();  
  11.         base.OnConfiguring(options);  
  12.     }  
  13. }  
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.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         using(var dataContext = new DataContext())  
  6.         {  
  7.             dataContext.Employee.Add(new Employee()  
  8.             {  
  9.                 BirthDate = DateTime.Now, FirstName = "Bhavik", LastName = "Patel"  
  10.             });  
  11.             dataContext.Employee.Add(new Employee()  
  12.             {  
  13.                 BirthDate = DateTime.Now, FirstName = "Sohil", LastName = "Patel"  
  14.             });  
  15.             dataContext.SaveChanges();  
  16.             foreach(var employee in dataContext.Employee.ToList())  
  17.             {  
  18.                 Console.WriteLine($ "Id= {employee.Id}, FirstName= {employee.FirstName}, LastName= {employee.LastName} and DateofBirth= {employee.BirthDate}");  
  19.             }  
  20.             Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  
Output

run

It will store two employees and retrieve from same. You can perform CRUD operations on the same running instance.

Download source code at Github.

My original article url.

References 

Up Next
    Ebook Download
    View all
    Learn
    View all