Moq.EntityFrameworkCore 10.0.0.2

dotnet add package Moq.EntityFrameworkCore --version 10.0.0.2
                    
NuGet\Install-Package Moq.EntityFrameworkCore -Version 10.0.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Moq.EntityFrameworkCore" Version="10.0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Moq.EntityFrameworkCore" Version="10.0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Moq.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Moq.EntityFrameworkCore --version 10.0.0.2
                    
#r "nuget: Moq.EntityFrameworkCore, 10.0.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Moq.EntityFrameworkCore@10.0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Moq.EntityFrameworkCore&version=10.0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Moq.EntityFrameworkCore&version=10.0.0.2
                    
Install as a Cake Tool

Moq.EntityFrameworkCore

Build and Test 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

Usage

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);

or

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

or

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

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);

Using ReturnsDbSetWithGlobalFilter

You can also use ReturnsDbSetWithGlobalFilter to set up a DbSet with a global filter. For example:

var users = new List<User> { new User { IsActive = true }, new User { IsActive = false } };
var userContextMock = new Mock<UsersContext>();
userContextMock.Setup(x => x.Users).ReturnsDbSetWithGlobalFilter(users, u => u.IsActive);

New Usage Option: ReturnsDbSetDynamic

You can now use the ReturnsDbSetDynamic method for scenarios where you need lazy evaluation of your DbSet. This ensures that the DbSet is re-evaluated dynamically at runtime.

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

Explanation of Differences

  • ReturnsDbSet: Statically resolves the DbSet value during setup.
  • ReturnsDbSetDynamic: Dynamically resolves the DbSet value using a lambda, ensuring it reflects changes at runtime.

You will find examples of this library in the repository.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

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

Package Downloads
mmx.appmodels

Package Description

Atacorp.ApiEssentials.Testing.XUnit

Package Description

Devocean.Tests

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

Atacorp.TestingEssentials.XUnit

Package Description

Eladei.Architecture.Tests.EntityFramework

Base types for building integration and unit tests focused on Entity Framework.

GitHub repositories (6)

Showing the top 6 popular GitHub repositories that depend on Moq.EntityFrameworkCore:

Repository Stars
matthewrenze/clean-architecture-demo
A sample app for my online course "Clean Architecture: Patterns, Practices, and Principles"
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
TanvirArjel/EFCore.GenericRepository
This repository contains Generic Repository implementation for Entity Framework Core
pkuehnel/TeslaSolarCharger
A software to let your Tesla charge with solar energy ☀
matthewrenze/clean-architecture-core
A sample app for my online course "Clean Architecture: Patterns, Practices, and Principles" in .NET Core
SapiensAnatis/Dawnshard
Server emulator for Dragalia Lost
Version Downloads Last Updated
10.0.0.2 86,967 4/15/2026
10.0.0 583,021 12/3/2025
9.0.0.10 111,728 12/3/2025
9.0.0.5 1,208,583 4/27/2025
9.0.0.1 809,312 1/10/2025
8.0.1.7 1,641,901 1/10/2025
8.0.1.6 6,884 1/10/2025
8.0.1.2 4,436,367 1/27/2024
8.0.1.1 165,140 1/27/2024
8.0.0.1 158,784 1/27/2024
7.0.0.2 3,781,850 12/1/2022
6.0.1.4 2,600,979 5/29/2022
6.0.1.3 190,686 5/29/2022
6.0.1.2 514,406 1/25/2022
6.0.0.6 125,871 1/25/2022
5.0.0.2 842,167 3/12/2021
5.0.0.1 134,658 11/29/2020
3.1.2.13 387,021 11/29/2020
3.1.2.6 185,723 6/29/2020
2.2.1.1 159,609 6/30/2020
Loading failed

Version 10.0.0.0
- Updated to .NET 10.0
- Updated to Entity Framework Core 10.0
- Updated async enumerator mock to use It.IsAny<CancellationToken>()
- Added comprehensive NuGet package configuration
- Added SourceLink support for debugging
- Enhanced package metadata for better discoverability
- Added symbol package generation (.snupkg)
- Improved build configuration for CI/CD
- Added .editorconfig for consistent code formatting
- Added Directory.Build.props for shared build properties