【问题标题】:Entity Framework Core 1.1 In Memory Database fails adding new entitiesEntity Framework Core 1.1 In Memory Database 无法添加新实体
【发布时间】: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);
        }
    }
}

【问题讨论】:

  • 你的模型和上下文是什么样的?

标签: entity-framework-core


【解决方案1】:

您可以通过使用 Collection Fixtures 来解决这个问题,这样您就可以在多个测试类中共享这个 Fixture。这样你就不会多次构建你的上下文,因此你不会得到这个异常:

Some information about collection Fixture

我自己的例子:

[CollectionDefinition("Database collection")]
public class DatabaseCollection : ICollectionFixture<DatabaseFixture>
{  }

[Collection("Database collection")]
public class GetCitiesCmdHandlerTests : IClassFixture<MapperFixture>
{
    private readonly TecCoreDbContext _context;
    private readonly IMapper _mapper;

    public GetCitiesCmdHandlerTests(DatabaseFixture dbFixture, MapperFixture mapFixture)
    {
        _context = dbFixture.Context;
        _mapper = mapFixture.Mapper;
    }

    [Theory]
    [MemberData(nameof(HandleTestData))]
    public async void Handle_ShouldReturnCountries_AccordingToRequest(
        GetCitiesCommand command,
        int expectedCount)
    {
        (...)
    }

    public static readonly IEnumerable<object[]> HandleTestData
        = new List<object[]>
        {
            (...)
        };
}

}

祝你好运, 赛博

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2018-12-10
    • 2013-07-21
    • 2020-02-06
    • 2017-10-27
    • 2017-06-17
    相关资源
    最近更新 更多