小编1ey*_*der的帖子

EF Core 内存数据库未保存到 DbSet

概述

我目前正在使用 EF Core 提供的内存数据库对我的存储库模式进行单元/集成测试。我使用的是 Microsoft.EntityFrameworkCore.InMemory nuget 版本 6.0.1,并使用 Visual Studio 2022。我在将数据保存到数据库时遇到问题。下面我包含了我的代码片段。

我的数据模型:

public class Example : IExample

public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
Run Code Online (Sandbox Code Playgroud)

我的基本存储库类:

public class Repository<T> : IRepository<T> where T : class

private readonly DbSet<T> _dbSet;
private readonly DbContext _db;

public Repository(DbContext db)
{
    _dbSet = db.Set<T>();
    _db = db;
}

public virtual async Task Add(T entity)
{
    await _dbSet.AddAsync(entity);
}

public virtual …
Run Code Online (Sandbox Code Playgroud)

c# integration-testing unit-testing entity-framework-core asp.net-core

5
推荐指数
0
解决办法
1572
查看次数