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.
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.
- public class Employee
- {
- [Key]
- 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 same as in the previous version.
- public class DataContext: DbContext
- {
- public DbSet < Employee > Employee
- {
- get;
- set;
- }
- protected override void OnConfiguring(DbContextOptionsBuilder options)
- {
- options.UseInMemoryDatabase();
- base.OnConfiguring(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"
- });
- dataContext.SaveChanges();
- foreach(var employee in dataContext.Employee.ToList())
- {
- Console.WriteLine($ "Id= {employee.Id}, FirstName= {employee.FirstName}, LastName= {employee.LastName} and DateofBirth= {employee.BirthDate}");
- }
- Console.ReadLine();
- }
- }
- }
Output 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