Store all enums as string even if they are defined as nullable

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      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 attached.

      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

            Assignee:
            Damien Guard
            Reporter:
            Jorge Ramirez
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: