Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF6:返回空集合的多对多关系_C#_Entity Framework_Entity Framework 6_Ef Code First - Fatal编程技术网

C# EF6:返回空集合的多对多关系

C# EF6:返回空集合的多对多关系,c#,entity-framework,entity-framework-6,ef-code-first,C#,Entity Framework,Entity Framework 6,Ef Code First,在过去的3个小时里,我一直在努力解决这个问题,我确信我错过了一些非常明显的东西 以下是型号代码: [Table("company")] public partial class company { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public company() {

在过去的3个小时里,我一直在努力解决这个问题,我确信我错过了一些非常明显的东西

以下是型号代码:

[Table("company")]
public partial class company
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public company()
    {
        job = new HashSet<job>();
        contact = new HashSet<contact>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int companyId { get; set; }

    [Required]
    [StringLength(255)]
    public string name { get; set; }

    [StringLength(255)]
    public string address { get; set; }

    [StringLength(255)]
    public string locality { get; set; }

    [StringLength(255)]
    public string city { get; set; }

    [StringLength(100)]
    public string state { get; set; }

    [StringLength(16)]
    public string postalCode { get; set; }

    [StringLength(2)]
    public string country { get; set; }

    [StringLength(16)]
    public string phone { get; set; }

    [StringLength(63)]
    public string email { get; set; }

    [StringLength(127)]
    public string website { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<job> job { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<contact> contact { get; set; }
}

[Table("contact")]
public partial class contact
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public contact()
    {
        this.company = new HashSet<company>();
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int contactId { get; set; }

    [Required]
    [StringLength(64)]
    public string firstName { get; set; }

    [Required]
    [StringLength(64)]
    public string lastName { get; set; }

    [StringLength(1)]
    public string middleInitial { get; set; }

    public string title { get; set; }

    [StringLength(18)]
    public string phoneNumber { get; set; }

    [StringLength(128)]
    public string emailAddress { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<company> company { get; set; }
}

[Table("job")]
public class job
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public job()
    {
        company = new HashSet<company>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int jobId { get; set; }

    public string jobTitle { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<company> company { get; set; }

}
我已在数据库端检查了以下所有内容:

  • 所有三个主表都存在,并与模型列相对应
  • 所有与桥接表的外键关系都存在
  • 两个桥接表都有一个由两列组成的复合主键
  • 两个桥接表的命名都与
    Map
    方法参数匹配,桥接表中的列也是如此
  • 数据库中有适当的数据,这些数据应使
    职务
    联系人
    实体显示在所有
    公司
    实体下
  • 手动写入数据库的查询确实成功地生成了预期的数据
然而:

ctx.company.Where(x => x.companyId == 300).job.Count()
始终返回
0
。尝试:

ctx.company.Where(x => x.companyId == 300).job.FirstOrDefault()
返回
null

我尝试在上下文中设置日志记录:

Database.Log = msg => System.Diagnostics.Debug.WriteLine(msg);
我注意到,发送到数据库的唯一查询是
公司
表。它不执行连接查询。它甚至从未触及
作业
联系人
表或桥接表。它只需使用正确的
WHERE
子句对
company
表进行一次查询,但不会进一步查询

为了好玩,我尝试在
.Map
调用中反转字段的顺序,但没有效果。(换句话说:
.Map(m=>m.ToTable(“公司工作”、“dbo”).MapLeftKey(“工作ID”).MapRightKey(“公司ID”))
没有改变任何事情。)

好的,我遗漏了什么?

确认

在上下文中,构造函数是这样的:

base.Configuration.ProxyCreationEnabled = false;
我把它取下来了,它现在似乎可以工作了


(我继承了这段代码,所以这里的教训是:永远不要假设您继承的代码与您认为的完全一样!)

您的查询不正确。实际查询是什么样子的?
base.Configuration.ProxyCreationEnabled = false;