'How to map a Id propertie with existing GUID in BsonClassMap with C# in MongoDb
try to map the id property from a derived class with a given GUID with BsonClassMap.RegisterClassMap() method.
public class SensorDefinition : IEntity, IAggregateRoot
{
public SensorDefinitionId Id { get; }
private string _name;
private string _description;
private string _version;
private int _numberChannels;
}
public class SensorDefinitionId : TypedIdValueBase
{
public SensorDefinitionId(Guid value) : base(value)
{
}
}
public abstract class TypedIdValueBase : IEquatable<TypedIdValueBase>
{
public Guid Value { get; }
protected TypedIdValueBase(Guid value)
{
if (value == Guid.Empty)
{
throw new InvalidOperationException("Id value cannot be empty!");
}
Value = value;
}
And the mapping class:
BsonClassMap.RegisterClassMap<SensorDefinition>(map =>
{
//map.AutoMap();
map.MapField("_name").SetElementName("Name");
map.MapField("_description").SetElementName("Description");
map.MapField("_version").SetElementName("Version");
map.MapField("_numberChannels").SetElementName("NumberChannels");
map.MapIdProperty(_ => _.Id).SetElementName("Id");
});
The problem: in the database there is no guid value in the ID.
How does it look like the correct map code for the id? thx.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
