实体框架加载相关对象而没有可用的导航属性(DTO)?

时间:2017-05-27 16:26:15

标签: vb.net entity-framework

我首先使用模型在EF中有一些对象,大多数对象是相关的:

Partial Public Class PersonLog
     Public Property Id As System.Guid
     Public Property ExternalEmailGuid As System.Guid
     Public Overridable Property Person As Person
End Class

Partial Public Class EmailIn
     Public Property Id As System.Guid
     Public Property PersonID As System.Guid
End Class

Partial Public Class EmailOut
     Public Property Id As System.Guid
     Public Property PersonID As System.Guid
End Class

这样使用Person对象我可以轻松获取相关的PersonLogs

 _person.PersonLogs.OrderByDescending(Function(c) c.CreateDate).ToList()

但是我需要获取与该PersonLog关联的关联EmailIn或EmailOut对象。他们之间没有任何关联,我不太清楚为什么。

有没有办法加载所有相关的EmailIn& EmailOut对象没有迭代每个PersonLog项并执行查找? PersonLog与EmailIn或EmailOut之间的关系为1:1

我正在考虑的方法是创建一个新类,它将当前上下文和PersonLog作为构造函数,并具有公共属性PersonLog,EmailIn和EmailOut,如下所示:

Public Class PersonLogDTO
Public Sub New(obj As PersonLog, cxt As DB.DBContainer)
    _obj = obj
    _cxt = cxt
End Sub
Public ReadOnly Property LeadLog As PersonLog
    Get
        Return _obj
    End Get
End Property

Public ReadOnly Property EmailOut As EmailOut
    Get
        Return _cxt.EmailOut.Find(_obj.ExternalEmailGuid)
    End Get
End Property

要做得更好,对吗?

0 个答案:

没有答案