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

  • And what is the reason to make string values ​​as keys? Maybe we should reconsider this moment, and not create a problem, and then try to solve it? - Bulson
  • I have already reviewed, there are options, but if there is a solution to my question, then that would be best. Of course, if there are more problems than advantages, no one will use such a solution. - Artem Hohryakov

0