Idea: Key entity, that can be replaced by other key, if that happens they get their FK's pointing to each other.
public class Key {
string Id;
string ReplacesId;
Key Replaces;
string ReplacedById;
Key ReplacedBy;
}
Fluent mapping
modelBuilder.Entity<Key>().HasOne(k => k.Replaces)
.WithOne()
.HasForeignKey<Key>(k => k.ReplacesId)
.IsRequired(false);
modelBuilder.Entity<NagKey>().HasOne(k => k.ReplacedBy)
.WithOne()
.HasForeignKey<NagKey>(k => k.ReplacedById)
.IsRequired(false);
Problem: Trying to save an entity, it fails with duplicate foreign key error (which is null) as follows:
System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.Keys' with unique index 'IX_Keys_ReplacedById'. The duplicate key value is (). The statement has been terminated.
Solved by making indexes not unique:
modelBuilder.Entity<Key>()
.HasIndex(x => x.ReplacedById)
.IsUnique(false)
.HasFilter(null);
modelBuilder.Entity<Key>()
.HasIndex(x => x.ReplacesId)
.IsUnique(false)
.HasFilter(null);