Skip to content

Instantly share code, notes, and snippets.

@eka808
Created July 4, 2013 09:24
Show Gist options
  • Save eka808/5926225 to your computer and use it in GitHub Desktop.
Save eka808/5926225 to your computer and use it in GitHub Desktop.
Make the Set entity framework method usable in unit tests
/// <summary>
/// Overload of the Set method of entity framework
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IDbSet<T> Set<T>() where T : class
{
var correspondingDbSets = this.GetType().GetProperties().Where(a => a.PropertyType == typeof(IDbSet<T>));
if (correspondingDbSets.Count() == 1)
{
var correspondingProperty = correspondingDbSets.FirstOrDefault();
return (IDbSet<T>)correspondingProperty.GetValue(this,null);
}
throw new Exception("Unexisting dbset");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment