2017-10-12 131 views
0

對於使用實體框架的多對多關係,我有一個相當簡單的問題。多對多對另一對多對多對單個實體

情況是這樣的我有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和許多到SectionLines在上下文中使用FluentApi相關和接線表:

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名單,我想這一個電話讓還SectionNames名單猛吃具體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; 
    } 

回答

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

請試試看,上面的代碼沒有經過測試。