So here is my issue...what I am trying to create is a self-referencing many to many relationship. Basically here is my model.
public class InformationSystem
{
public InformationSystem()
{
Systems = new HashSet<InformationSystem>();
ParentSystems = new HashSet<InformationSystem>();
}
[Key()]
public int InformationSystemID { get; set; }
public string InformationSystemName { get; set; }
//Navigation properties
public virtual ICollection<InformationSystem> Systems { get; set; }
public virtual ICollection<InformationSystem> ParentSystems { get; set; }
}
The idea being that a system can have many parents and a parent can have many children. I know how to do a self-referencing entity where many children can have one parent. What is tripping me up is the many to many part. Below is my DbContext.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<InformationSystem>(entity =>
{
entity
.HasMany(e => e.ParentSystems)
.WithMany(e => e.Systems)
.OnDelete(DeleteBehavior.Restrict);
});
However on my DbContext, I get an error that .WithMany does not contain a definition for with many that would accept an input of type collection. I know that basically what needs to be built is a link table when the code first creates a migration and updates the database. The link table I think would have two columns and no key. One column would be InformationSystemID and one would be ParentInformationSystemID. Both would be foreign keys. I also know that for this to work right, the delete behavior should be restrict so that if an entry is deleted or updated in the link table, that change won't cascade (and create a loop). Can someone please point me in the right direction of what I need to do to get EF Core 3 to do this correctly? If I had to create a link table myself, how would I go about doing that? And what would I need to do in my DbContext? I know that a link table would look something like this:
I'd greatly appreciate it.
public class InfoSysToParentInfoSys
{
public int InfoSysID;
public virtual InformationSystem InfoSys;
public int ParentInfoSysID;
public virtual InformationSystem ParentInfoSys;
}