There are two classes with many-to-many dependencies.
public class EquipmentTemplate { int Id { get; set; } string StringId { get; set; } // other properties... MaterialTemplate MaterialTemplates { get; set; } ICollection<MaterialTemplate> Materials { get; set; } } public class MaterialTemplate { int Id { get; set; } string StringId { get; set; } // other properties... EquipmentTemplate EquipmentTemplates { get; set; } ICollection<EquipmentTemplate> Equipments { get; set; } } It is necessary to make these classes, in the database in the derived table, refer to each other not by ID, but by StringId, while StringId is not a key in the class.
Tried through FluentApi like this:
modelBuilder.Entity<MaterialTemplate>() .HasMany(e => e.Equipments) .WithRequired(e => e.MaterialTemplates) .HasForeignKey(e => e.StringId) .WillCascadeOnDelete(false); modelBuilder.Entity<EquipmentTemplate>() .HasMany(e => e.Materials) .WithRequired(e => e.EquipmentTemplates) .HasForeignKey(e => e.StringId) .WillCascadeOnDelete(false); but swears that the key types do not match:
One or more validation errors were detected during the model: MaterialTemplate_Materials_Source_MaterialTemplate_Equipments_Target: The type of property "StringId" on the entity "EquipmentTemplate" does not match