.NET 6.0
Install-Package Moq.EntityFrameworkCore -Version
dotnet add package Moq.EntityFrameworkCore --version
<PackageReference Include="Moq.EntityFrameworkCore" Version="" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Moq.EntityFrameworkCore --version
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Moq.EntityFrameworkCore,"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Moq.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=Moq.EntityFrameworkCore&version=

// Install Moq.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=Moq.EntityFrameworkCore&version=
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


Build Status Downloads

This library helps you mocking EntityFramework contexts. Now you will be able to test methods that are using DbSet<TEntity> or DbQuery<TEntity> from DbContext in an effective way.

Installation - NuGet Packages

Install-Package Moq.EntityFrameworkCore


For example we can assume that we have the following production code:

public class UsersContext : DbContext
    public virtual DbSet<User> Users { get; set; }

To mock Users and Roles you only need to implement the following 3 steps:

1. Create DbContext mock:

var userContextMock = new Mock<UsersContext>();

2. Generate your entities:

IList<User> users = ...;

3. Setup DbSet or DbQuery property:

userContextMock.Setup(x => x.Users).ReturnsDbSet(users);


userContextMock.SetupGet(x => x.Users).ReturnsDbSet(users);


userContextMock.SetupSequence(x => x.Set<User>())
  .ReturnsDbSet(new List<User>())

And this is all. You can use your DbContext in your tests.

The second option is mocking DbSet that is part of the interface:

public interface IBlogContext
   DbSet<Post> Posts { get; }

And then use:

var posts = new List<Post>();
var contextMock = new Mock<IBlogContext>();
contextMock.Setup(p => p.Posts).ReturnsDbSet(posts);

You will find examples of this library in the repository.

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Moq.EntityFrameworkCore:

Package Downloads

Devocean.Tests is a set of base classes and helpers inteded to support implementing integration and unit tests on projects based on Devocean.Core

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated 38,476 5/29/2022 1,387 5/29/2022 159,536 1/25/2022 313 1/25/2022 301,983 3/12/2021 71,839 11/29/2020 129,850 11/29/2020 137,772 6/29/2020 69,399 2/26/2020 35,670 11/9/2019 6,358 10/6/2019 65,778 6/30/2020
2.2.1 139,801 2/1/2019
2.0.1 183,356 2/23/2018
1.0.0 6,012 10/13/2017

Added support for SetupGet