【发布时间】:2017-07-24 15:54:41
【问题描述】:
我在测试设置的单元测试中使用以下代码:
var simpleEntity = new SimpleEntity();
var complexEntity = new ComplexEntity
{
JoinEntity1List = new List<JoinEntity1>
{
new JoinEntity1
{
JoinEntity2List = new List<JoinEntity2>
{
new JoinEntity2
{
SimpleEntity = simpleEntity
}
}
}
}
};
var anotherEntity = new AnotherEntity
{
ComplexEntity = complexEntity1
};
using (var context = databaseFixture.GetContext())
{
context.Add(anotherEntity);
await context.SaveChangesAsync();
}
当达到 SaveChangesAsync 时,EF 会抛出带有以下消息的 ArgumentException:
已添加具有相同密钥的项目。键:1
我还为单元测试类使用了一个夹具,它使用相同类型的对象填充数据库,尽管对于这个测试我想要这个特定的设置,所以我想将这些新实体添加到内存数据库中。我尝试在 DbSet(而不是 DbContext)上添加实体并分别添加所有三个实体但无济于事。但是,我可以单独添加“simpleEntity”(因为它没有添加到夹具中),但只要我尝试添加“complexEntity”或“anotherEntity”,EF 就会抱怨。
似乎内存数据库中的 EF 无法处理不同上下文实例上的多个 Add。是否有任何解决方法或者我在设置中做错了什么?
本例中的databaseFixture是这个类的一个实例:
namespace Test.Shared.Fixture
{
using Data.Access;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
public class InMemoryDatabaseFixture : IDatabaseFixture
{
private readonly DbContextOptions<MyContext> contextOptions;
public InMemoryDatabaseFixture()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
var builder = new DbContextOptionsBuilder<MyContext>();
builder.UseInMemoryDatabase()
.UseInternalServiceProvider(serviceProvider);
contextOptions = builder.Options;
}
public MyContext GetContext()
{
return new MyContext(contextOptions);
}
}
}
【问题讨论】:
-
你的模型和上下文是什么样的?