I’m trying this out on a brand new project and i have this error on read:
System.FormatException: GuidSerializer cannot deserialize a Guid when GuidRepresentation is Standard and binary sub type is UuidLegacy.
at MongoDB.Bson.Serialization.Serializers.GuidSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonSerializationInfo.DeserializeValue(BsonValue value)
at MongoDB.EntityFrameworkCore.Serializers.SerializationHelper.TryReadElementValue[T](BsonDocument document, BsonSerializationInfo elementSerializationInfo, T& value)
at MongoDB.EntityFrameworkCore.Serializers.SerializationHelper.GetPropertyValue[T](BsonDocument document, IReadOnlyProperty property)
at lambda_method41(Closure, QueryContext, BsonDocument)
at MongoDB.EntityFrameworkCore.Query.QueryingEnumerable`2.Enumerator.MoveNextHelper()
at MongoDB.EntityFrameworkCore.Query.QueryingEnumerable`2.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.<>c.<<Main>$>b__0_2(NotificationContext dbContext) in C:\DevProjects\TestProjects\MongoDbGuidProblems\TestApplication\Program.cs:line 45
at lambda_method3(Closure, Object, HttpContext)
at Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
My repro is very simple:
`
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var mongodbConnectionString = builder.Configuration.GetConnectionString(“testdb”)!;
builder.Services.AddDbContext(options =>
{
options.UseMongoDB(mongodbConnectionString, “testdb”);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapPost(“/”, (Notification notification, NotificationContext dbContext) =>
{
notification.Id = Guid.NewGuid();
dbContext.Notifications.Add(notification);
dbContext.SaveChanges();
return Results.Created();
})
.WithName(“CreateNotification”)
.WithOpenApi();
app.MapGet(“/”, (NotificationContext dbContext) =>
{
var notifications = dbContext.Notifications.ToList();
return notifications;
})
.WithName(“GetNotificationList”)
.WithOpenApi();
var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService();
dbContext.Database.EnsureCreated();
app.Run();
public class NotificationContext(DbContextOptions options) : DbContext(options)
{
public DbSet Notifications => Set();
}
public class Notification
{
public Guid Id { get; set; }
public required string Message { get; set; }
public bool IsRead { get; set; }
}
`
What do i do wrong here? It works perfectly on 8.0.x. The EF Add methods saves the guid using bin format but it’s trying to read standard i think?