【发布时间】:2012-12-18 18:00:18
【问题描述】:
我正在尝试了解 EF,但遇到了障碍,我正在使用简单的博客/帖子结构并创建了类,例如:
''' <summary>
''' Represents a blog in the database
''' </summary>
''' <remarks></remarks>
Public Class Blog
Public Property BlogID As Integer
Public Property Name As String
''' <summary>
''' All posts in this blog
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property BlogPosts As List(Of BlogPost)
End Class
现在如果我这样做
Using db As New DBContext
_blog = db.Blogs.
FirstOrDefault(Function(m) m.BlogID = BlogID)
Dim _posts = _blog.BlogPosts.First
End Using
我在 BlogPosts 上得到一个空引用异常,这不是延迟加载。
现在我可以使用 .Include 强制它工作,但这会破坏对象。
我发现的所有 c# 示例都在 BlogPosts 属性中声明了“虚拟”,我不知道 VB 版本会是什么?例如:
public virtual ICollection<BlogPosts> BlogPosts { get; set; }
【问题讨论】:
标签: .net vb.net entity-framework