更新时间:2023-02-13 14:56:26
它为空,因为您尚未请求Entity Framework检索关联。
It's null because you haven't asked Entity Framework to retrieve the association.
有两种方法:
1 - 延迟加载 / p>
1 - Lazy Loading
var reciever = db.Recievers.SingleOrDefault();
var receiverPackets = receiver.Packets; // lazy call to DB - will now be initialized
我不喜欢这种方法,我个人关闭延迟加载,并使用其他方法
I don't like this approach, i personally turn off lazy loading, and use the other approach
2 - Eager Loading b
var receiver = db.Receivers.Include("Packets").SingleOrDefault();
这导致接收器和数据包之间的LEFT OUTER JOIN,而不是两个调用懒惰加载。
Which results in a LEFT OUTER JOIN between Receivers and Packets, instead of two calls - which is the case with lazy loading.
这是否能解答你的问题?
Does that answer your question?