I am using the MongoDB EF Core provider and I am getting duplicates for embedded collections of complex objects when I load the root object. I have noticed this with latest version 8.0 and previous 7.0.0-preview.1. I am using .net8. This is my .proj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.0.0" />
</ItemGroup>
</Project>
Here is the sample which triggers the bug:
using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.EntityFrameworkCore.Extensions;
var connectionString = Environment.GetEnvironmentVariable("MONGODB_URL");
var client = new MongoClient(connectionString);
var db = MflixDbContext.Create(client.GetDatabase("sample"));
var movie1 = new Movie();
movie1._id = ObjectId.GenerateNewId();
movie1.title = "Movie 1";
movie1.actors.Add(new Actor() { FirstName = "John", LastName = "Smith" });
movie1.actors.Add(new Actor() { FirstName = "Mary", LastName = "Smith" });
db.Movies.Add(movie1);
Console.WriteLine("Total actors going to the database: " + movie1.actors.Count());
db.SaveChanges();
var movieFromDb = db.Movies.FirstOrDefault(x => x._id == movie1._id);
Console.WriteLine("Movie: " + movieFromDb.title);
Console.WriteLine("Total actors after loading from database: " + movieFromDb.actors.Count());
internal class MflixDbContext : DbContext
{
public DbSet<Movie> Movies { get; init; }
public static MflixDbContext Create(IMongoDatabase database) =>
new(new DbContextOptionsBuilder<MflixDbContext>()
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
.Options);
public MflixDbContext(DbContextOptions options)
: base(options) {}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Movie>().ToCollection("movies");
}
}
internal class Actor
{
public string FirstName { get; set;}
public string LastName { get; set;}
}
internal class Movie
{
public ObjectId _id { get; set; }
public string title { get; set; }
public List<Actor> actors { get; set; } = new List<Actor>();
}
If you run, you will get the following output:
Total actors going to the database: 2
Movie: Movie 1
Total actors after loading from database: 4
If you look at the “movies” collection in mongodb, you will find no duplicates:
sample> db.movies.find()
[
{
_id: ObjectId('66396aa29571e499f39d6fab'),
title: 'Movie 1',
actors: [
{ FirstName: 'John', LastName: 'Smith' },
{ FirstName: 'Mary', LastName: 'Smith' }
]
}
]
I hope I am doing something wrong because this is a pretty big bug to be released. While 7.0.0 was preview release, 8.0 is not a preview release but a general release.
I have also noticed situations where only a portion of the children items are duplicated, instead of all of them.
Can anyone confirm they are having this issue? I will post this in the MongoDB forums also.