如何首先在实体框架4代码中建立复杂的多对多关系 [英] How to set up a complex many to many relationship in entity framework 4 code first

查看:47
本文介绍了如何首先在实体框架4代码中建立复杂的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在User对象和许多查找表之间建立相对复杂的关系。用户对象是您运行的工厂用户模型:

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; }
}

必需的徽章:

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; }
}

必需的徽章子要求:

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; }
}

这是一组查找,大约有四个嵌套类,例如这个,还有一些桌子下。查找表的总数约为16个。

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.

我最初在考虑是否使用RequirementLog模型进行绑定:

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; }
}

因此,RequiresLog和Youth之间有很多很多。在RequirementsLog之外创建的表具有一个PK列(ID)和每个属性的FK列。由此(RequirementsLogYouths)创建的多对多表有两个PK(RequirementsLogId和YouthId)。

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).

我要用正确的方法吗?最终目标是使16个左右的表服务器作为各种需求的列表,并具有另一个表以跟踪特定青年在这些需求中的进步。我很难想象这些DBA内容,所以任何输入都会受到赞赏。

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.

推荐答案

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");
        });            
    }

这篇关于如何首先在实体框架4代码中建立复杂的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆