Home > Entity Framework > Entity Framework 7: In Memory Testing

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.

install ef7 pre release using nuget

Alternatively the old-fashioned way

PM>Install-Package EntityFramework.InMemory -Prerelease

 

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.

Saved changes output to the console window

Advertisements
  1. August 26, 2016 at 9:20 am

    How does this work with guid’s as primary keys?
    A table in sql server would look like
    CREATE TABLE NEWID_TEST
    (
    ID UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
    TESTCOLUMN CHAR(2000) DEFAULT REPLICATE(‘X’,2000)
    )

    then the scaffolder does its job and then?
    Will the in memory provider do that all right?

  1. October 1, 2015 at 10:37 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: