How to set up a complex many to many relationship in entity framework 4 code first

落爺英雄遲暮 提交于 2019-12-11 19:48:06

问题


I have a relatively complex relationship I need to set up between a User object and a lot of lookup tables. The user object is your run of the mill user model:

public class Youth : IAuditInfo
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public Guid YouthGuid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
    public string ImageName { get; set; }
    [ForeignKey("FkYouthId")]
    public ICollection<User> Parents { get; set; }
    public CubPack Pack { get; set; }
    public virtual ICollection<RequirementsLog> RequirementsLogs { get; set; }
    public Youth()
    {
        Parents = new List<User>();
    }
}

The lookup tables is where it gets complex and I can't figure out the path of least complexity in binding them together. For the lookups it is a series of tables starting with one 'master' table, that rolls down hierarchically to requirements and sub requirements, like this:

Master:

public class BearTrail
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<BearTrailRequiredBadge> BearTrailRequiredBadges { get; set; }
    public virtual ICollection<BearTrailElectiveBadge> BearTrailElectivedBadges { get; set; }
}

Required Badges:

public class BearTrailRequiredBadge
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Number { get; set; }
    public string Description { get; set; }
    public virtual ICollection<BearTrailRequiredBadgeSubRequirement> BearTrailRequiredBadgeSubRequirements { get; set; }
}

Required Badge sub requirement:

public class BearTrailRequiredBadgeSubRequirement
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Number { get; set; }
    public string Text { get; set; }
    public bool Required { get; set; }
}

This is one set of the lookups, there are about four nested classes like this, and some one off tables as well. Total lookup tables is about 16, give or take.

I was initially thinking if using my RequirementLog model to bind it:

public class RequirementsLog
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public virtual ICollection<Youth> Youth { get; set; }
    public BearTrail BearTrailRequirements { get; set; }
    public TigerTrail TigerTrailRequirements { get; set; }
    public WolfTrail WolfTrailRequirements { get; set; }
    public WebelosTrail WebelosTrailRequirements { get; set; }
    public WebelosArrowOfLight WebelosArrowOfLightRequirements { get; set; }
}

So there is a many to many between RequirementsLog and Youth. The table created out of RequirementsLog has one PK column (ID), and FK columns for each property. The many to many table created out of this (RequirementsLogYouths) has two PKs (RequirementsLogId, and YouthId).

Am I going about this the right way? The end goal is to have the 16 or so tables server as just lists of various requirements, and have another table(s) to track a particular youths progress through the requirements. I have a hard time visualizes some of this DBA stuff, so any input would be greatly appreciated.


回答1:


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Entity<Entity1>()
        .HasMany(b => b.Entities2)
        .WithMany(p => p.Entities1)
        .Map(m =>
        {
            m.ToTable("Entitie1Entity2");
            m.MapLeftKey("Entity1Id");
            m.MapRightKey("Entity2Id");
        });            
    }



回答2:


In most cases, a requirements "log" be in a one (people) to many (the log).

Unless... One logged item is for many kids... If so, the you need a third table, that maps many people to multiple logged events. That is, if this is truly a many to many. In general, that situation almost always begs for a third, intermediate mapping table. Read up a bit on many to many designs, and you'll quickly see it, and how simple it is.



来源:https://stackoverflow.com/questions/19876979/how-to-set-up-a-complex-many-to-many-relationship-in-entity-framework-4-code-fir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!