首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多对多,多对多,在单个实体上

多对多,多对多,在单个实体上
EN

Stack Overflow用户
提问于 2017-10-12 10:32:26
回答 2查看 56关注 0票数 0

关于使用实体框架的许多关系,我有一个相当简单的问题。

情况如下:我有3种型号的SectionName:

代码语言:javascript
复制
public class SectionName : BaseEntity
{
    public SectionName()
    {
        SectionsSuffix = new List<SectionSuffix>();
    }

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

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

后缀:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
[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相关:

代码语言:javascript
复制
    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列表,我想用这个调用get列表也可以得到特定SectionSuffix的SectionNames列表,这有可能吗?

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

代码语言:javascript
复制
    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;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-13 06:21:19

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-10-12 16:33:23

如果我要质疑某事物。就像你想做的那样,会是这样的:

代码语言:javascript
复制
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();
    }
}

请试一试,上面的代码是没有测试的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46707384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档