Search code examples
c#mongodbentity-framework

MongoDB.Driver.MongoIncompatibleDriverException: Server reports wire version 4 but this version of the driver requires at least 7


I am trying to use Entity Framework and MongoDB.

This is my docker compose:

services:
  mongo-db:
    container_name: mongo-db
    image: mongo
    restart: always
    ports:
      - "27017:27017"

Entity and context:

public class Order
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class OrdersDbContext(DbContextOptions options) : DbContext(options)
{
    public DbSet<Order> Orders { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Order>().ToCollection("orders");
    }
}

This is how I am trying to use it:

var order = new Order
{
    Id = Helpers.GetNewId(),
    Name = "Some order"
};

var mongoClient = new MongoClient("mongodb://localhost:27017");
var dbContextOptions =
    new DbContextOptionsBuilder<OrdersDbContext>().UseMongoDB(mongoClient, "orders");
var ordersDb = new OrdersDbContext(dbContextOptions.Options);
ordersDb.Orders.Add(order);
ordersDb.SaveChanges();

When calling SaveChanges, I get this error:

Unhandled exception. MongoDB.Driver.MongoIncompatibleDriverException: Server at localhost:27017 reports wire version 4, but this version of the driver requires at least 7 (MongoDB 4.0.0).

at MongoDB.Driver.MongoIncompatibleDriverException.ThrowIfNotSupported(ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServerHelper.SelectServer()
at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.IClusterExtensions.SelectServerAndPinIfNeeded(IClusterInternal cluster, ICoreSessionHandle session, IServerSelector selector, IReadOnlyCollection1 deprioritizedServers, CancellationToken cancellationToken) at MongoDB.Driver.Core.Bindings.WritableServerBinding.GetWriteChannelSource(IReadOnlyCollection1 deprioritizedServers, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Bindings.WritableServerBinding.GetWriteChannelSource(CancellationToken cancellationToken) at MongoDB.Driver.Core.Bindings.ReadWriteBindingHandle.GetWriteChannelSource(CancellationToken cancellationToken)

That makes no sense to me, since the actual mongodb version in container is 8.0.3. Any ideas how to fix it?


Solution

  • Most-likely, you're wrong, the error says that your server is 3.2 which is very old. You can see it if you run buildInfo command from the shell (like mongosh): (see here)

    db.runCommand( { buildInfo: 1 } )