Uploaded image for project: 'Entity Framework'
  1. Entity Framework
  2. EF-97

Unable to use a nullable complex datatype with on a class if the document does not include the property.

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

      This is an example of a model that I'm trying to use. 

      MongoDb Document:

       

      {
        "_id": {
          "$oid": "65d96a518c225137f2d2aee4"
        },
        "Name": "Test 1",
        "Places": [
          "Place 1",
          "Place 2"
        ],
        "LoginInfo": {
          "Website": "string",
          "UserName": "string",
          "Password": "string"
        }
      } 

      C# Class

      [Collection("tests")]
      [BsonIgnoreExtraElements]
      public class Test{
          public ObjectId Id { get; set; }    
          public string? Name { get; set; }    
          public List<string>? Places { get; set; }
          [BsonIgnoreIfNull]    
          public LoginInfo LoginInfo { get; set; } = null!;
      } 

       LoginInfo Class

       

       

      public class LoginInfo{     
          public string? Website { get; set; }     
          public string? UserName { get; set; }     
          public string? Password { get; set; }
      } 

      A large driver for me selecting this database type was the flexibility of fields on Documents. Currently if I do not provide a "LoginInfo" property on each of my documents, then it returns:

      System.Collections.Generic.KeyNotFoundException: Element 'LoginInfo' not found. 

      If I provide even and empty object named "LoginInfo" then it works and correctly maps null to the properties which I've defined as null. I have tried making the property nullable, Ignoring extra elements, ignoring the bson if null, using the BsonClassMap, specifiying the BsonElement and even initializing a new LoginInfo if its null. None of which work.

      Below is the package versions I'm using as well

      <Project Sdk="Microsoft.NET.Sdk.Web">  
      <PropertyGroup>
       <TargetFramework>net8.0</TargetFramework>    
       <Nullable>enable</Nullable>        
       <ImplicitUsings>enable</ImplicitUsings>  
      </PropertyGroup>  
      <ItemGroup>    
       <PackageReference Include="MongoDB.Driver" Version="2.24.0"/>     <PackageReference Include="MongoDB.EntityFrameworkCore" Version="7.0.0-preview.1"/>    
       <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14"/>      <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0"/>    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>  </ItemGroup>
      </Project> 

       

            Assignee:
            damien.guard@mongodb.com Damien Guard
            Reporter:
            keaton.grant.lee@gmail.com Keaton Lee
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: