Archive
Entity Framework 7: In Memory Testing
I am really excited about the in-memory testing feature of Entity Framework 7 as testing in the previous versions of Entity Framework wasn’t straight forward and painful at times .
Let’s get started by installing the Entity Framework “InMemory” testing nuget package and make sure you have selected the “Include Prerelease” in the nuget dialogue as Entity Framework 7 is still in beta 6.
Alternatively the old-fashioned way
As you can see that when we install the EntityFramework.InMemory package it also brings the EntityFramework.Core package.These are some of the new changes to Entity Framework as EF has been decomposed into smaller manageable packages.
Lets start writing our domain objects and I am going to use the simple example of Student and Course as show below.
Student.cs
public class Student { public Student() { Courses = new HashSet<Course>(); } public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public ICollection<Course> Courses { get; set; } public override string ToString() { return string.Format( "Id = {0}, FirstName = {1}, LastName = {2}", this.Id, this.FirstName, this.LastName); } }
Course.cs
public class Course { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } }
Next we will define the database context and this is where you will see a significant change to the previous version. In the constructor of our context we are creating a dependency on DbContextOptions which will decide which source to use to persist the data.
TrainingContext.cs
public class TrainingContext : DbContext { public TrainingContext(DbContextOptions options) : base(options) { } public DbSet<Student> Student { get; set; } public DbSet<Course> Course { get; set; } }
Now we will wire up the TrainingContext to the main program and using the DbContextOptionsBuilder we will instruct the entity framework to use the in-memory database. I also created a separate method to populate the student table with 2 records.
Program.cs
class Program { static void Main(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<TrainingContext>(); optionsBuilder.UseInMemoryDatabase(true); using (var context = new TrainingContext(optionsBuilder.Options)) { AddStudentData(context); var student = context.Student.FirstOrDefault(x => x.Id == 1); Console.WriteLine(student.ToString()); student.FirstName = "James"; context.SaveChanges(); student = context.Student.FirstOrDefault(x => x.Id == 1); Console.WriteLine(student.ToString()); Console.ReadLine(); } } private static void AddStudentData(TrainingContext context) { context.Student.Add(new Student { Id = 1, FirstName = "Joe", LastName = "Blogg" }); context.Student.Add(new Student { Id = 2, FirstName = "Jane", LastName = "Doe" }); context.SaveChanges(); } }
As you can see we first retrieve the student with id 1 then we changed the first name of the student and asked entity framework to save the changes into the in-memory database.
And below is the output of the before and after state of that record.