Description:
When creating a class that has a custom convert over the Id, and another property with another converter, an error occures
NotSupportedException: Alternate keys are not supported by the MongoDB EF Core Provider.
Example of class
public class A : TrackableEntity<AId> { public A(AId id) : base(id) { } public AT Typo { get; set; } // This causes the error public string T { get; set; } = string.Empty; } public record AId(Guid Value) : EntityId(Value); public record AT(string Value);
Model builder
public class EventsDbContext(DbContextOptions<EventsDbContext> options) : BaseDbContext<EventsDbContext>(options) { protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<A>() .HasKey(a => a.Id); modelBuilder.Entity<A>() .Property(a => a.Id) .HasConversion(a => a.Value, a => new AId(a)) .HasElementName("_id"); // Also without adding the property with the conversion, the error still occures modelBuilder.Entity<A>() .Property(x => x.Typo) .HasConversion(type => type.Value, type => new AT(type)) .HasElementName("type"); modelBuilder.Entity<A>() .Property(a => a.T) .HasColumnName("t") .HasMaxLength(100) .IsRequired(); base.OnModelCreating(modelBuilder); }}