0

I'm having a difficult time trying to map out my relationships with EF code first. We're creating a "Lookup" table for key value pairs. This table also has a type to define the type of lookup. These will be used in a number of dropdown lists in the app. The table will basically have Id, Name, Type. Some of the larger entities may have multiple lookup values. Below is an example. There is also a many to many relationship that needs to exist.

public partial class SystemDetail : EntityData
{
    public string BrandId { get; set; }
    [ForeignKey("BrandId")]
    public Lookup Brand { get; set; }

    public string OperatingSystemId { get; set; }
    [ForeignKey("OperatingSystemId")]
    public Lookup OperatingSystem { get; set; }

    public string BatteryTypeId { get; set; }
    [ForeignKey("BatteryTypeId")]
    public Lookup BatteryType { get; set; }

    public ICollection<Lookup> RemovableStorageDevices { get; set; }
}

public class Lookup : EntityData
{
    [MaxLength(125)]
    public string Name { get; set; }
    public LookupTypeEnum LookupType { get; set; }

    public ICollection<SystemDetail> SystemDetails { get; set; }
}

Is this even possible to accomplish?

DennisM
  • 49
  • 1
  • 5
  • I answered a similar question here: http://stackoverflow.com/questions/29333787/how-to-create-lookup-table-and-define-relationships You could also look at some of the EF inheritance stuff (table per hierarchy) http://stackoverflow.com/questions/4927350/ef-table-per-hierarchy-mapping – Steve Greene Jan 15 '16 at 20:55
  • The problem arises when trying to add the many to many to the lookup table. Have you run across this scenario? – DennisM Jan 15 '16 at 21:36
  • We don't do it that way - no navigation collections are added on the lookup side (don't need to navigate from say hair color to a collection of persons). I just need to assign a key value pair on my main table. In theory it's many to many capable, but we just use it as a one to many. – Steve Greene Jan 15 '16 at 21:47
  • I believe I may have found it – DennisM Jan 15 '16 at 21:58
  • Will have to see if it works, but this appears to do it: modelBuilder.Entity().HasMany(s => s.RemovableStorageDevices).WithMany(r => r.SystemDetails); Thanks for pointing me in the right direction – DennisM Jan 15 '16 at 22:29

0 Answers0