Entity Framework Core / MongoDB: the entity type ‘TestEntity’ primary key property ‘Id’ must be mapped to element ‘_id’

I am writing a data access layer which is tested against multiple database implementations; some SQL (Postgres, Sqlite, SQL Server) and some document (MongoDB). In order to test different databases, my test fixtures include a test entity, entity configuration and DbContext.

For example:

TestEntity:

public sealed class TestEntity(Guid id, string text, int number, DateTime created)
{
    public TestEntity(string text, int number) : this(Guid.Empty, text, number, DateTime.UtcNow)
    {
    }

    public Guid Id { get; } = id;
    public string Text { get; } = text;
    public int Number { get; } = number;
    public DateTime Created { get; } = created;
    public Optional<DateTime> Updated { get; private init; } = Optional<DateTime>.None;
    public bool IsUpdated => Updated.HasValue;

    public TestEntity Update() => new(Id, Text, Number, Created) { Updated = DateTime.UtcNow };
    public override string ToString() => this.ToRecordString();
}

TestEntityTypeConfiguration:

public sealed class TestEntityTypeConfiguration : IEntityTypeConfiguration<TestEntity>
{
    public void Configure(EntityTypeBuilder<TestEntity> builder)
    {
        builder.HasKey(entity => entity.Id);
        builder.Property(entity => entity.Id).ValueGeneratedOnAdd();
        builder.Property(entity => entity.Text).IsRequired().HasMaxLength(32);
        builder.Property(entity => entity.Number).IsRequired();
        builder.Property(entity => entity.Created).IsRequired();
        builder.Property(entity => entity.Updated).HasConversion<OptionalValueConverter<DateTime>>();
        builder.Ignore(entity => entity.IsUpdated);
    }
}

TestDbContext:

public sealed class TestDbContext(DbContextOptions<TestDbContext> options) : DbContext(options)
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new TestEntityTypeConfiguration());
    }
}

Note that the integration tests work for all SQL databases. Only MongoDB is failing with the following error:

The entity type ‘TestEntity’ primary key property ‘Id’ must be mapped to element ‘_id’

Given that I am testing against multiple database implementations, I cannot modify TestEntity, TestEntityTypeConfiguration, or TestDbContext without good reason, as I do not want the changes to impact integration tests currently working against SQL databases.

The MongoDB test class is as follows:

IsolatedRepositoryMongoDbIntegrationTests:

public sealed class IsolatedRepositoryMongoDbIntegrationTests(ITestOutputHelper output) : IsolatedRepositoryIntegrationTests(output)
{
    private readonly MongoDbContainer container = new MongoDbBuilder()
        .WithUsername("test_user")
        .WithPassword("test_password")
        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(27017))
        .Build();

    protected override void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<TestDbContext>(ConfigureDatabase);
        services.AddSingleton<IIsolatedRepository<Guid, TestEntity>, TestIsolatedRepository>();
    }

    private void ConfigureDatabase(DbContextOptionsBuilder builder)
    {
        IMongoClient client = new MongoClient(container.GetConnectionString());
        builder.UseMongoDB(client, "test_db");
    }

    public override async Task InitializeAsync()
    {
        await container.StartAsync();
    }

    public override async Task DisposeAsync()
    {
        await container.StopAsync();
        await container.DisposeAsync();
    }
}

Note that IsolatedRepositoryMongoDbIntegrationTests inherits the actual test cases from IsolatedRepositoryIntegrationTests, therefore this class only needs to contain the configuration for MongoDB.

Have I missed some configuration somewhere, or is there some configuration I should add to IsolatedRepositoryMongoDbIntegrationTests in order to make the tests pass?

I have searched for the error but haven’t found any documentation relating directly to MongoDB and Entity Framework Core, so if you know of anything I should read, please let me know.

Thank you.

PS: if you downvote, please explain why.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật