【问题标题】:Entity Framework One to One navigation property not loading实体框架一对一导航属性未加载
【发布时间】:2014-02-20 18:53:07
【问题描述】:

所以我有一个简单的数据库,其中包含两个表,首先使用实体​​框架 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();            
    }

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

谢谢

【问题讨论】:

  • Company 真的是null 还是一个未初始化的对象?我看到您在 Submission 构造函数 (this.Company = new tbl_lst_Company();) 中实例化了导航属性,这并不好。无论如何,您都应该删除此行。但是,我不确定它是否会在这里解决您的特定问题。
  • 实际的Company类不为null,但是一旦展开,里面的所有属性都为null。
  • 取出 Company 的实例化,当使用调试器查看事物 Company 变量时抛出此错误:'(a[0]).Company' 抛出了 'System.Data.Entity.Core 类型的异常.EntityCommandExecutionException'
  • 是否有内部异常可以提供有关此 EntityCommandExecutionException 的更多详细信息?在您的示例中,x 怎么样?它是预期的公司还是 null 还是您在该行也遇到异常?
  • 谢谢,我想我明白了。实际上,实例化单个导航属性会导致项目在延迟加载时始终为空。

标签: c# sql entity-framework


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

所以我想通了,有很多地方出了问题,但这是对我有用的解决方案。您不需要实例化单个导航属性。这将导致它始终为空。如果导航属性是对象的 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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多