0

Hallo I have a situation that I have a problem with:

I Have 3 models lets say Device, Inbox and Suffix. Device can have only one Inbox and Inbox can be assign to many devices And Inbox and Suffix are connected with many to many relationship by junction table.

fluent api for inbox(SectionName) and sugffix would look like:

            modelBuilder.Entity<SectionName>()
            .HasMany(suffix => suffix.SectionsSuffix)
            .WithMany(name => name.SectionsName)
            .Map(nameSuffix =>
            {
                nameSuffix.ToTable("SectionsNameSuffix");
                nameSuffix.MapLeftKey("SectionNameId");
                nameSuffix.MapRightKey("SectionSuffixId");
            });

Models are nothing special:

Inbox:

[Table("SectionsName")]
public class SectionName : BaseEntity
{
    public SectionName()
    {
        SectionsSuffix = new List<SectionSuffix>();
    }

    [Required]
    public string Name { get; set; }

    public ICollection<SectionSuffix> SectionsSuffix { get; set; }
}

Suffix:

[Table("SectionsSuffix")]
public class SectionSuffix : BaseEntity
{
    public SectionSuffix()
    {
        SectionLines = new List<SectionLine>();
        SectionsName = new List<SectionName>();
    }

    [Required]
    public string Name { get; set; }

    public ICollection<SectionLine> SectionLines { get; set; }
    public ICollection<SectionName> SectionsName { get; set; }
}

And device:

public class Device
{
    protected Device()
    {
    }

    public int Id { get; set; }

    public string Description { get; set; }

    public int? InboxId { get; set; }

    [ForeignKey("InboxId")]
    public virtual SectionName Inbox { get; set; }
}

So basically one device can be connected to single combination of inbox and suffix

Suffix is connected to another many to many but that's just for information.

Now, I am looking for a way to when I get records from device entity will know witch combination is used. I was thinking about adding additional column to junction table, but would that complicate things, and I wonder if there is a another way to do it.

Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • Can `Device` be connected to exactly the same combinations of inbox and suffix that are modelled in the many-many relationship or does it model a superset / subset / independent relation? – grek40 Oct 17 '17 at 11:48
  • if i understand correctly is Device and Inbox has One to Many relationship and Inbox and suffix has Many to Many relationship ? – RJ- Oct 17 '17 at 12:21
  • @RJ- yes that is the situation – Wojciech Szabowicz Oct 17 '17 at 13:36
  • @Wojciech Szabowicz you may consider to have a look on https://stackoverflow.com/questions/16490334/how-to-define-many-to-many-relationship-through-fluent-api-entity-framework – RJ- Oct 17 '17 at 14:03

1 Answers1

0

Putting id combination in main table did the trick.

Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87