Spencer

Spencer

  • 1.7k
  • 25
  • 861

Foreign Key Relationships.

Nov 29 2023 6:49 PM

Hopefully, one of you more experienced developers can help with this (I'm still learning)

I have an issue with a shadow key being created, I have this class;

public class ProductInterest
{
    public int Id { get; set; }

    public int ProductInterestId { get; set; }

    [Required(ErrorMessage = "Product Interest Name is required")]
    [DisplayName("Product of Interest")]
    public string ProductInterestName { get; set; }
}

 The data this form collects I would like to use this as a dataset in two dropdown lists, so i created this;

public class Lead
{
    //other properties

    [Display(Name = "Product Interest")]
    public int? ProductInterestId { get; set; }

    [Display(Name = "Product Interest 2")]
    public int? ProductInterestId2 { get; set; }

    public ProductInterest? ProductInterest { get; set; }

    public List<ProductInterest>? ProductInterestsList { get; set; }
    public List<ProductInterest>? ProductInterests2List { get; set; }

    //other lists

}

after the migration, I got the error;

"There are multiple relationships between 'ProductInterest' and 'Lead' without configured foreign key properties. This will cause Entity Framework to create shadow properties on 'ProductInterest' with names dependent on the discovery order. Consider configuring the foreign key properties using the [ForeignKey] attribute or in 'OnModelCreating'.
Microsoft.EntityFrameworkCore.Model.Validation[10625]"

I have tried 

public class Lead
{
    // ... other properties ...

    public int? ProductInterestId { get; set; }
    [ForeignKey("ProductInterestId")]
    public ProductInterest ProductInterest { get; set; }

    public int? ProductInterestId2 { get; set; }
    [ForeignKey("ProductInterestId2")]
    public ProductInterest ProductInterest2 { get; set; }

    // ... other properties ...
}

But got the same result, so after many hours I gave up and updated the database to see what would happen and noticed in the database that it had created columns in the dataset table (product interest) two columns for LeadId1 & LeadId2. I don't need this to be logged here as the Lead table is catching which selection has been made.

After a little digging and some help from a well-known AI helper, I tried this;

modelBuilder.Entity<Lead>()
    .HasOne(l => l.ProductInterest)
    .WithMany()
    .HasForeignKey(l => l.ProductInterestId)
    .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Lead>()
    .HasOne(l => l.ProductInterest2)
    .WithMany()
    .HasForeignKey(l => l.ProductInterestId2)
    .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Lead>()
    .HasOne(l => l.LeadSource)
    .WithMany()
    .HasForeignKey(l => l.LeadSourceId)
    .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Lead>()
    .HasOne(l => l.LeadType)
    .WithMany()
    .HasForeignKey(l => l.LeadTypeId)
    .OnDelete(DeleteBehavior.Restrict);

Although the program is working fine I know that it's not correct and, on this long and at times painful journey of learning I would like to try and understand what I'm doing wrong

 

any help or advice would be appreciated.

 


Answers (3)