我们正面临着EF的问题。 当延迟加载,有时时,导航属性将返回null。
注意:我在db上下文中创建了this.Configuration.LazyLoadingEnabled = true;
型号:
public class Student
{
public Int64 Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public Int64 Address_Id { get; set; }
[ForeignKey("Address_Id")]
public virtual Address Address { get; set; }
}
public class Address
{
public Int64 Id { get; set; }
public string Name { get; set; }
}
数据库访问:
Student oStudnet = context.Students.FirstOrDefault();
Int64 addressId= oStudnet.Address.Id
" null exception" on" oStudnet.Address.id" //这里的地址为空
答案 0 :(得分:-2)
Student oStudnet = context.Students.Include("Address").FirstOrDefault();
然后你可以访问地址