Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架一对一导航属性未加载_C#_Sql_Entity Framework - Fatal编程技术网

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

C# 实体框架一对一导航属性未加载,c#,sql,entity-framework,C#,Sql,Entity Framework,因此,我有一个简单的数据库,其中包含两个表,首先使用EntityFramework6.02使用代码进行设置。提交表与tbl_lst_Company表具有一对一关系。它们由公司和公司ID字段关联 public partial class Submission { public Submission() { this.Company = new tbl_lst_Company(); } public int Keytbl { ge

因此,我有一个简单的数据库,其中包含两个表,首先使用EntityFramework6.02使用代码进行设置。提交表与tbl_lst_Company表具有一对一关系。它们由公司和公司ID字段关联

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字段总是空的。如果我在db.Submissions.Include(“Company”)中明确地说来自s,那么Company字段不是空的,但我必须加载导航属性。我希望公司导航属性被延迟加载。据我所知,我正在按预期的方式做每件事,但它不起作用。我做错了什么


谢谢

据我所知,您希望填充
x
,但不希望填充
a
中每个提交的公司

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


所以我发现,有很多事情是错误的,但这里有一个解决方案对我来说是有效的。您不需要实例化单个导航属性。这将导致它始终为空。如果导航属性是对象的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");
    }
}
公共部分类提交
{       
public int Keytbl{get;set;}
公共整数公司{get;set;}
公共虚拟tbl_lst_公司tbl_lst_公司{get;set;}
}
公共部分类tbl_lst_公司
{       
公共tbl_lst_公司(){
this.Submissions=新列表();
}
public int CompanyID{get;set;}
公共字符串公司{get;set;}
公共虚拟ICollection提交{get;set;}
}
公共tbl公司地图()
{
//主键
this.HasKey(t=>t.CompanyID);
//性质
this.Property(t=>t.Company)
.IsRequired()
.HasMaxLength(150);
//表和列映射
本表为ToTable(“tbl_lst_公司”);
this.Property(t=>t.CompanyID).HasColumnName(“CompanyID”);
此.Property(t=>t.Company).HasColumnName(“公司”);
}
公开提交地图()
{
//主键
this.HasKey(t=>t.Keytbl);
//表和列映射
本表为“提交文件”;
this.Property(t=>t.Keytbl).HasColumnName(“Keytbl”);
此.Property(t=>t.Company).HasColumnName(“公司”);
this.has可选(t=>t.tbl\lst\u公司)
.有许多(t=>t.提交)
.HasForeignKey(d=>d.Company);
}
[测试方法]
公共无效测试\u惰性\u加载(){
使用(var db=new SubmissionContext()){
var subs=(来自b.提交文件中的s
选择s);
var x=子公司First().tbl公司;
AreEqual(x,null,“延迟加载失败”);
}
}

公司是否真的是
空的
还是未初始化的对象?我看到您在
Submission
构造函数(
this.Company=new tbl\u lst\u Company();
)中实例化了导航属性,这不太好。在任何情况下都应删除此行。但是,我不确定它是否能解决您在这里遇到的特定问题。实际的Company类不是null,但一旦扩展,其中的所有属性都是null。去掉Company的实例化,在使用调试器查看thing Company变量时抛出此错误:'(a[0]).Company“引发了类型为”System.Data.Entity.Core.EntityCommandExecutionException“的异常”是否存在内部异常,这些异常会说明有关此EntityCommandExecutionException的更多详细信息?您的示例中的
x
如何?这是预期的公司还是空的,或者你在这一行也得到了一个例外?谢谢,我想我已经解决了。实际上,实例化单个导航属性会导致项目在延迟加载时始终为空。我希望它通过延迟加载加载公司引用。正如我所理解的延迟加载,无论何时访问该变量,它都应该加载它。现在,如果我可以让它在不使用Include选项的情况下工作,我会很高兴在提交中加载每个公司。你确定该文档没有过时,是从2008年开始的吗?我读到的所有其他内容都表明您不需要使用Load。我认为这个例子是显式加载的:您使用的是什么版本的EF?也许这会有所帮助?
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");
    }
}