[英]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: 我已经在数据库端检查了以下所有内容:
Map
method parameters, as do the columns in the bridge tables. Map
方法参数匹配,桥表中的列也是如此。 job
and contact
entities to appear under all company
entities. job
和contact
实体出现在所有company
实体下。 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.