实体框架一对一导航属性未加载

时间:2014-02-20 18:53:07

标签: c# sql entity-framework

所以我有一个简单的数据库,其中两个表首先使用实体​​框架6.02设置代码。 Submission表与tbl_lst_Company表具有一对一的关系。它们与公司和CompanyID字段相关。

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

以下是Fluent映射:

    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");
    }

这是我正在运行的单元测试,用于测试上述实现:

    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();            
    }

我的问题是,当我运行此测试时,Company字段始终为null。如果我说明db.Submissions.Include(“Company”)中的s,那么公司字段不是null但我必须急于加载导航属性。我希望公司导航属性延迟加载。据我所知,我正在按照预期的方式做所有事情,但它无法正常工作。我做错了什么?

由于

2 个答案:

答案 0 :(得分:1)

据我了解,您希望x已填充,但您不希望在a中填写每个提交的公司。

您可以告诉它加载第一个公司,然后使用它。

var first = subs.First();
first.CompanyReference.Load();
var x = first.Company;
var a = subs.ToArray();

答案 1 :(得分:1)

所以我想通了,有很多错误,但这里有适合我的解决方案。您不需要实例化单个导航属性。这将导致它始终为null。如果它是对象的ICollection,您仍然需要实例化导航属性。还有其他一些小事。谢谢你的帮助。

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");
    }
}