[英]Navigation properties not loading properly
My context looks like: 我的上下文如下:
public class ApplicationDbContext: IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
this.Configuration.LazyLoadingEnabled = true;
}
//DbSet properties
}
so, lazy loading is enabled. 所以,启用了延迟加载。
I have following class: 我有以下课程:
public class Home
{
private ICollection<Slide> _slides;
[Key]
[Required]
public string Name { get; set; }
[ForeignKey("Header")]
public int? HeaderID { get; set; }
//Navigation properties
public ICollection<Slide> Slides
{
get { return _slides ?? (_slides = new List<Slide>()); }
set { _slides = value; }
}
public Content Header { get; set; }
}
Note that both navigation properties Header
and Slides
are used without virtual
keyword. 请注意,导航属性Header
和Slides
都使用没有 virtual
关键字。 As far as I know when we don't use virtual
keyword - properties should load eagerly. 据我所知,当我们不使用virtual
关键字时 - 属性应该急切加载。
However, when I get my Home
entity from database, both my navigation properties are null
(but property HeaderID
has value). 但是,当我从数据库获取我的Home
实体时,我的两个导航属性都为null
(但属性HeaderID
具有值)。
Even if I switch to this.Configuration.LazyLoadingEnabled = false;
即使我切换到this.Configuration.LazyLoadingEnabled = false;
- preperties not loaded - they still null
. - 未加载的属性 - 它们仍为null
。
Here is how I get my data from db (using repository pattern): 以下是我从db获取数据的方法(使用存储库模式):
public static Home GetHomeComponent(
this IRepositoryAsync<Home> repository)
{
var result = repository
.Query()
.Select()
.First();
return result;
}
I solved my problem with Include
properties: 我用Include
属性解决了我的问题:
public static Home GetHomeComponent(
this IRepositoryAsync<Home> repository)
{
var result = repository
.Query()
.Include(x => x.Header)
.Include(x=>x.Slides)
.Select()
.First();
return result;
}
However it's not convenient for me (since I have too much navigation properties to load). 但是这对我来说不方便(因为我有太多的导航属性要加载)。
So, my question is: 所以,我的问题是:
I don't use virtual
keyword - but why my navigation properties not loading eagerly? 我不使用virtual
关键字 - 但为什么我的导航属性没有急切加载?
Or I'm doing something wrong? 或者我做错了什么? Is there any other way to load my navigation properties without using Include
? 有没有其他方法加载我的导航属性而不使用Include
?
If you don't use the virtual
keyword it only means that once you try to access the non-virtual property it wont be loaded from the database but you will get a Null
. 如果你不使用virtual
关键字,它只意味着一旦你尝试访问非虚拟属性,它将不会从数据库加载,但你会得到一个Null
。
It doesn't mean you will have all the properties of the entities populated right away, to populate Slides
for EG in your code, you have to use .Include() - this is eager loading, to load the property by your self before it used . 这并不意味着你将立即填充实体的所有属性,在你的代码中填充Slides
for EG,你必须使用.Include() - 这是急切的加载,在你之前加载你自己的属性用过的 。
You can make a generic function that will populate the required properties by the arguments it gets ( using params ) see here for more details : 您可以创建一个通用函数,通过它获取的参数填充所需的属性(使用params),请参阅此处了解更多详细信息:
EntityFramework Eager Load all Navigation Properties EntityFramework Eager加载所有导航属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.