Click here to Skip to main content
15,895,142 members
Articles / DevOps / Unit Testing

Two Strategies for Testing Entity Framework - Effort and SQL CE

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
19 Sep 2012CPOL6 min read 117.9K   2.5K   27  
Two approaches for unit testing Entity Framework
In this article, you will learn about two approaches for unit testing Entity Framework: Mocking in memory by using "Effort", and against a real database using SQLCE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;

namespace TestingEf.Data.Entities
{
    public static class MyAppContextExtensions
    {
		/// <summary>
		/// Adds Include functionality from EntityFramework to IDbSet collections. 
        /// Include paths will be applied if the <paramref name="dbSet"/> is found to be an actual
        /// DbSet, otherwise include paths are ignored.
		/// </summary>
		/// <typeparam name="T">The type of entity that the DbSet applies to</typeparam>
		/// <param name="dbSet">The DbSet to apply the Include paths to</param>
		/// <param name="includes">The navigation paths to include</param>
		/// <returns>An IQueryable of the original DbSet with the include paths applied</returns>
        public static IQueryable<T> Include<T>(this IDbSet<T> dbSet, params string[] includes) where T : class
        {
            if (dbSet is DbSet<T>)
            {
                DbQuery<T> query = (DbQuery<T>)dbSet;

                foreach (var include in includes)
                {
                    query = query.Include(include);
                }

                return query;
            }

            return dbSet;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions