Ala*_*n B 5 c# entity-framework xunit.net entity-framework-core .net-core
我正在使用 EF Core 2.0.0 和 InMemory 2.0.0 创建 xunit 测试。我注意到实体没有添加到上下文中。然而它是在 context..Local 中添加的
下面是代码片段
public UnitOfWorkTest()
{
_appointment = new Appointment
{
AppointmentType = AppointmentTypes.EyeTest,
AppProgress = Appointment.Confirmed,
BranchIdentifier = "MEL",
DateAdded = DateTime.Now,
Duration = 30,
Resid = "KAI",
};
}
public MyDbContext InitContext()
{
var options = new DbContextOptionsBuilder<MyDbContext>()
.UseInMemoryDatabase("Add_writes_to_database")
.Options;
return new MyDbContext(options);
}
public async Task UnitOfWork_Transaction_Test()
{
using (var context = InitContext())
{
using (var unitOfWork = new UnitOfWork(context))
{
context.Appointment.Add(_appointment);
await unitOfWork.Commit();
Assert.True(context.Appointment.Local.Count == 1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
public sealed class UnitOfWork : IUnitOfWork
{
private IDbContext _dbContext;
public UnitOfWork(IDbContext context)
{
_dbContext = context;
}
public async Task<int> Commit()
{
// Save changes with the default options
return await _dbContext.SaveChangesAsync();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (_dbContext != null)
{
_dbContext.Dispose();
_dbContext = null;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
public interface IDbContext : IDisposable
{
DbSet<TEntity> Set<TEntity>() where TEntity : class;
EntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
EntityEntry Entry(object entity);
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken));
}
Run Code Online (Sandbox Code Playgroud)
context.Appointment总是返回空列表/null,但我可以在中看到添加的实体context.Appointment.Local
知道为什么会发生这种情况吗?如何获取添加到 Appointment 集合中而不是 Appointment.Local 集合中的实体?
| 归档时间: |
|
| 查看次数: |
2380 次 |
| 最近记录: |