Dealing with collection type in MongoDb provider efcore
I recently tried out the new provider for EF Core (MongoDb)
The model I got
When I insert the programming language with the versions list mongodb add the new document just fine
But when I try to retrieve data from the database
there's an exception below
[Collection("ProgrammingLanguages")]
public class ProgrammingLanguage : BaseEntity
{
public string Name { get; set; } = string.Empty;
public string Logo { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string? URL { get; set; }
public DateTime ReleaseDate { get; set; }
public ICollection<ProgrammingLanguageVersion>? Versions { get; set; }
}
[Collection("ProgrammingLanguages")]
public class ProgrammingLanguage : BaseEntity
{
public string Name { get; set; } = string.Empty;
public string Logo { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string? URL { get; set; }
public DateTime ReleaseDate { get; set; }
public ICollection<ProgrammingLanguageVersion>? Versions { get; set; }
}
public class ProgrammingLanguageVersion
{
public string? URL { get; set; }
public DateTime ReleaseDate { get; set; }
}
public class ProgrammingLanguageVersion
{
public string? URL { get; set; }
public DateTime ReleaseDate { get; set; }
}
//Insert code
context.ProgrammingLanguages.Add(new ProgrammingLanguage()
{
Name = "Test Programming Lang 2",
Versions = new List<ProgrammingLanguageVersion>()
{
new()
{
ReleaseDate = DateTime.Now,
URL = "bruh"
}
}
});
context.ChangeTracker.DetectChanges();
Console.WriteLine(context.ChangeTracker.DebugView.LongView);
await context.SaveChangesAsync();
//Insert code
context.ProgrammingLanguages.Add(new ProgrammingLanguage()
{
Name = "Test Programming Lang 2",
Versions = new List<ProgrammingLanguageVersion>()
{
new()
{
ReleaseDate = DateTime.Now,
URL = "bruh"
}
}
});
context.ChangeTracker.DetectChanges();
Console.WriteLine(context.ChangeTracker.DebugView.LongView);
await context.SaveChangesAsync();
var result = context.ProgrammingLanguages;
var result = context.ProgrammingLanguages;
2 Replies
Exception
System.InvalidOperationException: Type of ExtraElements member must be BsonDocument or implement IDictionary<string, object>.
at MongoDB.Bson.Serialization.BsonClassMap.SetExtraElementsMember(BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.Attributes.BsonExtraElementsAttribute.Apply(BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.Conventions.AttributeConventionPack.AttributeConvention.Apply(BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.Conventions.ConventionRunner.Apply(BsonClassMap classMap)
at MongoDB.Bson.Serialization.BsonClassMap.AutoMapClass()
at MongoDB.Bson.Serialization.BsonClassMap.AutoMap()
at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
System.InvalidOperationException: Type of ExtraElements member must be BsonDocument or implement IDictionary<string, object>.
at MongoDB.Bson.Serialization.BsonClassMap.SetExtraElementsMember(BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.Attributes.BsonExtraElementsAttribute.Apply(BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.Conventions.AttributeConventionPack.AttributeConvention.Apply(BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.Conventions.ConventionRunner.Apply(BsonClassMap classMap)
at MongoDB.Bson.Serialization.BsonClassMap.AutoMapClass()
at MongoDB.Bson.Serialization.BsonClassMap.AutoMap()
at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
i guess you have to show BaseEntity