单个实体上的多对多对多对多

时间:2017-10-12 10:32:27

标签: c# entity-framework ef-fluent-api

我对使用实体框架的多对多关系提出了相当简单的问题。

情况看起来像这样我有3个模型SectionName:

public class SectionName : BaseEntity
{
    public SectionName()
    {
        SectionsSuffix = new List<SectionSuffix>();
    }

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

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

部分后缀:

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

和SectionLines:

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

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

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

现在SectionsName与SectionsSuffix有很多对多关系,并且在使用FluentApi和联结表的上下文中与SectionLines有很多相关:

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

        modelBuilder.Entity<SectionSuffix>()
            .HasMany(line => line.SectionLines)
            .WithMany(suffix => suffix.SectionsSuffix)
            .Map(nameSuffix =>
            {
                nameSuffix.ToTable("SectionsSuffixLines");
                nameSuffix.MapLeftKey("SectionSuffixId");
                nameSuffix.MapRightKey("SectionLinesId");
            });
    }

现在,如果我在调用SectionsNames时没有问题,我可以获得SectionsSuffix列表,我想用这一个调用获取binnames列表到特定的SectionSuffix,是否可能?

现在使用存储库模式过程如下所示:

    IList<SectionName> sections = SectionRepository.GetAll(x => x.SectionsSuffix).ToList();

    public virtual IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includes)
    {
        IQueryable query = includes.Aggregate(_dbSet.AsQueryable(), (current, include) => current.Include(include));
        return (IEnumerable<T>) query;
    }

2 个答案:

答案 0 :(得分:0)

如果我要查询某事。就像你想做的那样,就像那样:

using System;
using System.Data.Entity;

public class SectionRepository
{
    private readonly _context;


    public SectionRepository(IMyDbContext context)
    {
        _context = context
    }

    public ICollection<SectionName> GetAll()
    {
        return _context.SectionNames
            .Include(sn => sn.SectionsSuffix.SectionLine)
            .Select(sn => sn).ToList();
    }
}

请试一试,上面的代码未经过测试。

答案 1 :(得分:0)

答案相当简单,我需要使用:

IList<SectionName> sections = SectionRepository.GetAll(name => name.SectionsSuffix, 
name => name.SectionsSuffix.Select(suffix => suffix.SectionLines)).ToList();

public virtual IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includes)
{
    IQueryable query = includes.Aggregate(_dbSet.AsQueryable(), (current, include) => current.Include(include));
    return (IEnumerable<T>) query;
}