I’m looking for an easy way to set a global configuration in my DbContext to store as string any nullable or non-nullable Enumerations.
I’ve already tried with this:
configurationBuilder.Properties<Enum>().HaveConversion<string>();
Unfortunately if the Enum is defined as Enum? the code above throws the following:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
at MongoDB.EntityFrameworkCore.Serializers.ValueConverterSerializer`2.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize(IBsonSerializer serializer, BsonSerializationContext context, Object value)
at MongoDB.EntityFrameworkCore.Storage.MongoUpdate.WriteNonKeyProperties(IBsonWriter writer, IUpdateEntry entry, Func`2 propertyFilter)
at MongoDB.EntityFrameworkCore.Storage.MongoUpdate.WriteEntity(IBsonWriter writer, IUpdateEntry entry, Func`2 propertyFilter)
at MongoDB.EntityFrameworkCore.Storage.MongoUpdate.ConvertAdded(IUpdateEntry entry)
at MongoDB.EntityFrameworkCore.Storage.MongoUpdate.Create(IUpdateEntry entry)
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<OfTypeIterator>d__66`1.MoveNext()
at MongoDB.EntityFrameworkCore.Storage.MongoUpdateBatch.<CreateBatches>d__7.MoveNext()
at MongoDB.EntityFrameworkCore.Storage.MongoClientWrapper.<SaveUpdatesAsync>d__10.MoveNext()
at MongoDB.EntityFrameworkCore.Storage.MongoDatabaseWrapper.<SaveChangesAsync>d__3.MoveNext()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__111.MoveNext()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__115.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
at Program.<<Main>$>d__0.MoveNext() in C:\Repos\CustomersApp\Program.cs:line 29
Code used to reproduce the issue:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Testcontainers.MongoDB" Version="3.9.0" />
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.0.3" />
</ItemGroup>
</Project>
Program.cs
using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using MongoDB.Driver;
using Testcontainers.MongoDb;
Console.WriteLine("Starting the test container:");
await using var mongoContainer = new MongoDbBuilder()
.WithImage("mongo:6.0")
.Build();
await mongoContainer.StartAsync();
Console.WriteLine("Inserting some documents:");
Console.WriteLine(mongoContainer.GetConnectionString());
var mongoClient = new MongoClient(mongoContainer.GetConnectionString());
await using (var context = new CustomersContext(mongoClient))
{
var willow = new Customer
{
Id = ObjectId.GenerateNewId(),
Name = "Willow",
Species = null
};
context.Add(willow);
await context.SaveChangesAsync();
}
Console.ReadKey();
public class CustomersContext : DbContext
{
private readonly MongoClient _client;
public CustomersContext(MongoClient client)
{
_client = client;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseMongoDB(_client, "efsample");
public DbSet<Customer> Customers => Set<Customer>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<Enum>().HaveConversion<string>();
}
}
public class Customer
{
public required ObjectId Id { get; set; }
public required string Name { get; set; }
public Species? Species { get; set; }
}
public enum Species
{
Human,
Dog,
Cat
}
EF Core version: Microsoft.EntityFrameworkCore 8.0.6
Database provider: MongoDB.EntityFrameworkCore 8.0.3
MongoDB: MongoDb.Driver 2.27.0
Target framework: .NET 8
Operating system: Visual Studio 2022 17.10.4