简体   繁体   English

EF6:多对多关系返回空集合

[英]EF6: Many-to-many relationship returning empty collections

I've been trying to figure this out for the past 3 hours, and I'm sure I'm missing something very obvious... 在过去的3个小时中,我一直在努力弄清楚这一点,并且我肯定会漏掉一些非常明显的东西...

Here is the model code: 这是模型代码:

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

}

And in OnModelCreating I have this: OnModelCreating我有这个:

modelBuilder.Entity<company>()
    .HasMany(e => e.job)
    .WithMany(e => e.company)
    .Map(m => m.ToTable("company_job", "dbo").MapLeftKey("companyId").MapRightKey("jobId"));

modelBuilder.Entity<company>()
    .HasMany(e => e.contact)
    .WithMany(e => e.company)
    .Map(m => m.ToTable("company_contact", "dbo").MapLeftKey("companyId").MapRightKey("contactId"));

I've checked all of the following on the database end: 我已经在数据库端检查了以下所有内容:

  • All three main tables exist and match the model column-for-column. 所有这三个主表都存在并且与模型列对应。
  • All foreign key relationships to the bridge tables exist. 存在与桥表的所有外键关系。
  • Both bridge tables have a composite primary key consisting of the two columns. 两个桥表都有一个由两列组成的复合主键。
  • Both bridge tables are named matching the Map method parameters, as do the columns in the bridge tables. 两个桥表都被命名为与Map方法参数匹配,桥表中的列也是如此。
  • The database has appropriate data that should cause job and contact entities to appear under all company entities. 该数据库具有适当的数据,这些数据应导致jobcontact实体出现在所有company实体下。
  • Manually written queries to the database do succeed in producing the expected data. 手动写入数据库查询确实可以成功生成预期数据。

However: 然而:

ctx.company.Where(x => x.companyId == 300).job.Count()

always returns 0 . 始终返回0 Trying: 试:

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

returns null . 返回null

I tried setting up logging on the context: 我尝试在上下文上设置日志记录:

Database.Log = msg => System.Diagnostics.Debug.WriteLine(msg);

And I noticed that the only queries ever sent to the database are for the company table. 而且我注意到,曾经发送到数据库的唯一查询是针对company表的。 It doesn't do a join query. 它不执行联接查询。 It never even touches the job or contact tables, or the bridge tables. 它甚至都不会碰到job表, contact表或桥表。 It simply makes a single query to the company table with the correct WHERE clause but never goes further. 它只是使用正确的WHERE子句对company表进行单个查询,但WHERE再也没有进行。

Just for kicks I tried reversing the order of the fields in the .Map call, but that had no effect. 只是为了踢一下,我试图反转.Map调用中字段的顺序,但这没有任何效果。 (in other words: .Map(m => m.ToTable("company_job", "dbo").MapLeftKey("jobId").MapRightKey("companyId")) didn't change things.) (换句话说: .Map(m => m.ToTable("company_job", "dbo").MapLeftKey("jobId").MapRightKey("companyId"))并未更改。)

Ok, what am I missing? 好吧,我想念什么?

ACK... ACK ...

In the context constructor was this: 在上下文构造函数中是这样的:

base.Configuration.ProxyCreationEnabled = false;

I removed it and it seems to work now! 我删除了它,现在好像可以了!

(I inherited this code, so the lesson here is: never assume that code you inherit does exactly what you think it'll do!) (我继承了此代码,因此这里的教训是:永远不要假设您继承的代码确实执行了认为会做的事!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM