多对多关系使用实体框架6返回0元素集合

时间:2017-01-20 07:53:05

标签: c# entity-framework

我有一个可能很简单的问题,我正在尝试使用实体框架和流畅的api创建多对多的关系,而我的问题是,当我尝试在调试中执行任何查询或查看对象时,它始终为0项。

我使用的联结表如下:

enter image description here

所以关系存在,确定已经检查过:

选择candidate.firstname,skillset.name 来自候选人 在candidate.id = candidate_skillset.candidate_id上​​加入candidate_skillset 在candidate_skillset.skillset_id = skillset.id

上加入技能组

显示已加入的结果。

现在我的上下文如下:

public class CatalogContexct : DbContext
{
    public DbSet<Candidate> Candidates { get; set; }
    public DbSet<SkillSet> SkillSets { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
        .Map(m =>
        {
            m.ToTable("candidate_skillset");
            m.MapLeftKey("candidate_id");
            m.MapRightKey("skillset_id");
        });

        modelBuilder.Entity<SkillSet>().ToTable("skillset");
        modelBuilder.Entity<Candidate>().ToTable("candidate");
    }
}

我的左侧模特候选人:

[Table("candidate")]
public class Candidate
{
    public Candidate()
    {
        this.SkillSets = new HashSet<SkillSet>();
    }

    [Key]
    public int id { get; set; }

    [Column("firstname")]
    public string Firstname { get; set; }

    public int? commendation_id { get; set; }
    [ForeignKey("commendation_id")]
    public Commendation commendation { get; set; }

    public ICollection<SkillSet> SkillSets { get; set; }
}

我的右侧模特技能组合:

[Table("skillset")]
public class SkillSet : SimpleDictionary
{
    public SkillSet()
    {
        this.Candidates = new HashSet<Candidate>();
    }

    public virtual ICollection<Candidate> Candidates { get; set; }
}

并且该模型有一个父类:

public class SimpleDictionary
{
    [Key]
    public int id { get; set; }

    [Column("name")]
    public string Name { get; set; }
}

所以一切都应该有效,但是当我做的时候:

        var ca = this._catalog.Candidates
            .Include("SkillSets").Include("commendation").
            FirstOrDefault(x => x.SkillSets.Any());

结果为null,当我在属性技能组的调试集合上查看对象时,allays有0个元素,不知道它有什么问题吗?

1 个答案:

答案 0 :(得分:1)

我在你问题中用这里提到的相同结构尝试了这个并在本地尝试。我可以使用此代码获取数据。请尝试这个,让我知道这是否有帮助。为简单起见,我只省略了表彰表。

        var context = new SampleDbContext();

        var candidates = context.Candidates
            .Include("SkillSets").ToList();


        foreach (var candidate in candidates)
        {
            foreach (var sk in candidate.SkillSets.Where(  s1 => s1.Candidates.Count(c=>c.id == candidate.id)>0 ))
            {
                Console.WriteLine( string.Format(@" Name : {0} Skill :{1}",candidate.Firstname ,sk.Name )  );
            }
        }

以下是我的DbContext和其他实体类

 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class SampleDbContext : DbContext
    {
        public SampleDbContext()
            : base("name=SampleDBConnection")
        {
            this.Configuration.LazyLoadingEnabled = false;
        }

        public DbSet<Candidate> Candidates { get; set; }
        public DbSet<SkillSet> SkillSets { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
        .Map(m =>
        {
            m.ToTable("candidate_skillset");
            m.MapLeftKey("candidate_id");
            m.MapRightKey("skillset_id");
        });

            modelBuilder.Entity<SkillSet>().ToTable("skillset");
            modelBuilder.Entity<Candidate>().ToTable("candidate");
        }
    }

    [Table("candidate")]
    public class Candidate
    {
        public Candidate()
        {
            this.SkillSets = new HashSet<SkillSet>();
        }

        [Key]
        public int id { get; set; }

        [Column("firstname")]
        public string Firstname { get; set; }

        public int? commendation_id { get; set; }

        //[ForeignKey("commendation_id")]
        //public Commendation commendation { get; set; }

        public ICollection<SkillSet> SkillSets { get; set; }
    }

    public class SimpleDictionary
    {
        [Key]
        public int id { get; set; }

        [Column("name")]
        public string Name { get; set; }
    }

    [Table("skillset")]
    public class SkillSet : SimpleDictionary
    {
        public SkillSet()
        {
            this.Candidates = new HashSet<Candidate>();
        }

        public virtual ICollection<Candidate> Candidates { get; set; }
    }


}

您提到的查询的输出和我的代码的结果都匹配,我希望这是您想要的。

enter image description here