实体框架一对一导航属性不加载 [英] Entity Framework One to One navigation property not loading
问题描述
所以我有一个简单的数据库包含两个表的设置与代码首先使用实体框架6.02。在提交表具有与tbl_lst_Company表以一对一的关系。他们是由该公司和CompanyID领域相关的。
So I have a simple database with two tables setup with code first using entity framework 6.02. The Submission table has a one to one relationship with the tbl_lst_Company table. They are related by the company and CompanyID fields.
public partial class Submission
{
public Submission()
{
this.Company = new tbl_lst_Company();
}
public int Keytbl { get; set; }
public int companyid { get; set; }
public virtual tbl_lst_Company Company { get; set; }
}
public partial class tbl_lst_Company
{
public int CompanyID { get; set; }
public string Company { get; set; }
public virtual Submission Submission { get; set; }
}
和这里是流利的映射:
public SubmissionMap()
{
// Primary Key
this.HasKey(t => t.Keytbl);
// Table & Column Mappings
this.ToTable("Submissions");
this.Property(t => t.Keytbl).HasColumnName("Keytbl");
this.Property(t => t.companyid).HasColumnName("company");
this.HasRequired(q => q.Company).
WithOptional().Map(t => t.MapKey("Company"));
}
public tbl_lst_CompanyMap()
{
// Primary Key
this.HasKey(t => t.CompanyID);
// Properties
this.Property(t => t.Company)
.IsRequired()
.HasMaxLength(150);
// Table & Column Mappings
this.ToTable("tbl_lst_Company");
this.Property(t => t.CompanyID).HasColumnName("CompanyID");
this.Property(t => t.Company).HasColumnName("Company");
}
下面是我正在测试上面的实现我的单元测试:
Here is my unit test I am running to test the above implementation:
public void download_data() {
var db = new SubmissionContext();
db.Configuration.LazyLoadingEnabled = true;
var subs = (from s in db.Submissions
select s).Take(100);
var x = subs.First().Company;
var a = subs.ToArray();
}
我的问题是,公司字段总是空当我运行这个测试。如果我explicilty从s在db.Submissions.Include(「本公司」)说,后来公司字段不为空,但我不得不急于负载的导航属性。我希望公司的导航属性延迟加载。至于我可以告诉大家,我做的一切它是假设做的方式,但它不工作。我在做什么错了?
My problem is that the Company field is always null when I run this test. If I explicilty say from s in db.Submissions.Include("Company"), then the Company field is not null but I have to eager load the navigation property. I want the Company navigation property to lazy load. As far as I can tell I am doing everything the way it is suppose to be done but it is not working. What am I doing wrong?
感谢
推荐答案
所以我想通出来,有一些东西错了,但这里是为我工作的解决方案。你并不需要实例化一个单一的导航属性。这将导致它总是空。您仍然需要实例化的导航属性,如果它是一个对象的ICollection。还有一些其他的小事情为好。感谢您的帮助。
So I figured it out, there were a number of things wrong, but here is the solution that worked for me. You do not need to instantiate a single navigation property. This will cause it to be always null. You still need to instantiate the navigation property if it is an ICollection of objects. There were a few other minor things as well. Thanks for the help.
public partial class Submission
{
public int Keytbl { get; set; }
public int Company { get; set; }
public virtual tbl_lst_Company tbl_lst_Company{ get; set; }
}
public partial class tbl_lst_Company
{
public tbl_lst_Company() {
this.Submissions = new List<Submission>();
}
public int CompanyID { get; set; }
public string Company { get; set; }
public virtual ICollection<Submission> Submissions { get; set; }
}
public tbl_lst_CompanyMap()
{
// Primary Key
this.HasKey(t => t.CompanyID);
// Properties
this.Property(t => t.Company)
.IsRequired()
.HasMaxLength(150);
// Table & Column Mappings
this.ToTable("tbl_lst_Company");
this.Property(t => t.CompanyID).HasColumnName("CompanyID");
this.Property(t => t.Company).HasColumnName("Company");
}
public SubmissionMap()
{
// Primary Key
this.HasKey(t => t.Keytbl);
// Table & Column Mappings
this.ToTable("Submissions");
this.Property(t => t.Keytbl).HasColumnName("Keytbl");
this.Property(t => t.Company).HasColumnName("Company");
this.HasOptional(t => t.tbl_lst_Company)
.WithMany(t => t.Submissions)
.HasForeignKey(d => d.Company);
}
[TestMethod]
public void test_lazy_loading() {
using (var db = new SubmissionContext()) {
var subs = (from s in b.Submissions
select s);
var x = subs.First().tbl_lst_Company;
Assert.AreEqual(x, null, "Lazy Loading Failed");
}
}
这篇关于实体框架一对一导航属性不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!