1

我在我的单元测试项目(xUnit)中使用 Effort 2.2.13.0。在夹具中,我将公司数据添加到内存数据库中的公司表中。

CompanyId = 1, Name = "Company 1"
CompanyId = 2, Name = "Company 2"
CompanyId = 3, Name = "Company 3"

奇怪的行为是,在调用 SaveChanges 将数据存储到数据库后,公司 2 的 id 为 3,公司 3 的 id 为 2。

夹具:

public class SharedDatabaseFixture
    {
        public SharedDatabaseFixture()
        {
            var connection = DbConnectionFactory.CreateTransient();
            Context = new TestDbContext(connection);

            Seed();
        }

        public TestDbContext Context { get; }

        private void Seed()
        {
            var addresses = AddressSeed.Get();
            var countries = CountrySeed.Get();
            var stores = StoreSeed.Get();
            var companies = CompanySeed.Get();
            var products = ProductSeed.Get();

            Context.Countries.AddRange(countries);
            Context.Companies.AddRange(companies);
            <!-- companies list is still correct -->
            Context.SaveChanges();
            <!-- Company 2 has id 3, company 3 has id 2 -->
        }
    }

我在公司列表中预设了 id,我也尝试将它们留空,但结果是一样的。以前有人见过这种奇怪的行为吗?

4

0 回答 0