Hello, I can’t get the BsonClassMap right because it keeps generating the following error “An Array value cannot be written to the root level of a BSON document.”.
What do you recommend for mapping a context similar to the following?
I execute in initial application
EntityBaseMap.Entity<Product>();
ProductMap.Configure();
BsonClassMap
public class EntityBaseMap
{
public static void Configure<T>() where T : Entity<T>
{
BsonClassMap.RegisterClassMap<Entity<T>>(map =>
{
map.AutoMap();
map.SetIsRootClass(true);
map.SetIgnoreExtraElements(true);
map.MapIdMember(x => x.Id);
map.MapMember(x => x.CreateDate).SetIsRequired(true);
map.MapMember(x => x.EditDate);
map.MapMember(x => x.DeleteDate);
map.MapMember(x => x.Active).SetIsRequired(true);
});
}
}
public class ProductMap
{
public static void Configure()
{
BsonClassMap.RegisterClassMap<Product>(map =>
{
map.AutoMap();
map.SetDiscriminator("product");
map.SetIsRootClass(true);
map.MapMember(x => x.Description).SetIsRequired(true);
});
}
}
Entities
public interface IEntityBase
{
public Guid Id { get; }
}
public abstract class Entity<T> : AbstractValidator<T>, IEntityBase where T : Entity<T>
{
[JsonIgnore]
public Guid Id { get; protected set; }
[JsonIgnore]
public DateTime CreateDate { get; set; }
[JsonIgnore]
public DateTime? EditDate { get; set; }
[JsonIgnore]
public DateTime? DeleteDate { get; set; }
[JsonIgnore]
public bool Active { get; set; }
public abstract bool IsValid();
[NotMapped]
public ValidationResult ValidationResult { get; set; }
protected Entity()
{
ValidationResult = new ValidationResult();
Id = Guid.NewGuid();
CreateDate = DateTime.Now;
Active = true;
}
public override bool Equals(object obj)
{
var compareTo = obj as Entity<T>;
if (ReferenceEquals(this, compareTo)) return true;
if (ReferenceEquals(null, compareTo)) return false;
return Id.Equals(compareTo.Id);
}
public static bool operator ==(Entity<T> a, Entity<T> b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Equals(b);
}
public static bool operator !=(Entity<T> a, Entity<T> b)
{
return !(a == b);
}
public override int GetHashCode()
{
return (GetType().GetHashCode() * 907) + Id.GetHashCode();
}
public override string ToString()
{
return GetType().Name + "[Id = " + Id + "]";
}
}
public class Product : Entity<Product>
{
public string Description { get; private set; }
public Product(string description)
{
Description = description;
}
public Product(Guid id, string description)
{
Id = id;
Description = description;
}
public override bool IsValid()
{
return true;
}
}