Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
Unknown discriminator value MongoDB

I basically want a collection that saves multiple types of objects/documents but all of them inherit from a base interface.. However, I am keep getting this exception when loading:

Additional information: An error occurred while deserializing the Inventory property of class Character: Unknown discriminator value 'TestItem'.

Here is my codes:

public class Character
{
    ...
    public List<IInventoryItem> Inventory { get; set; }
    ...
}

public interface IInventoryItem
{
...
}

class TestItem : IInventoryItem
{
...
}

Help is very appreciated.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is that you don't explicitly declare your polymorphic types to MongoDB driver.

For that, you have two options: using attributes, or a mapping configuration method. More info here: http://mongodb.github.io/mongo-csharp-driver/2.2/reference/bson/mapping/

Using attributes:

[BsonDiscriminator("TestItem")]
public class TestItem : IInventoryItem
{
    ...
} 

Using mapping configuration method:

BsonClassMap.RegisterClassMap<TestItem>(); // do it before you access DB

WHY: documents that represent polymorphic types (e.g. interfaces) are saved to the DB with a special field named _t, which contains discriminator value. In your case, the Character document will look like this:

{
    // ... other fields
    Inventory: [
        {
            _t: "TestItem",
            //... other fields
        }, 
        {
            _t: "TestItem",
            //... other fields
        }
    ]
    // ... other fields
}

On write, if the mappings don't exist, they are created on the fly, and TestItem discriminator is mapped to TestItem class. From that moment on, the mappings exist for the life of the AppDomain. Thus for example, if you perform reads after writes, it will be OK.

But if you read data first thing, BSON serializer doesn't know what type the TestItem discriminator maps to. So it is good practice to always explicitly specify discriminator mappings. In this way you can also control discriminator values - by default they equal to type name.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda