首先是EF6数据库 - 由于类型不可用,因此未加载关系

时间:2015-05-14 21:19:23

标签: entity-framework entity-framework-6.1

我在Web应用程序中首次使用EF6数据库。有一个实体涉及两个1对多关系(本例中的E2):
E1< --- E2 ---> E3

E1和E3都有E2的导航属性(称为E2s)。当我尝试使用E3的ListView时,我收到标题中列出的错误。如果我从E3中删除E2的导航属性,一切正常!我试过整个谷歌都无济于事。任何帮助/方向将不胜感激。

于5/15添加:

tmg - 希望这就是你想要的。

这将是' E2'在我的例子中:

Imports System
Imports System.Collections.Generic

Partial Public Class contact
    Public Property contact_id As Integer
    Public Property implementation_id As Integer
    Public Property contact_type_id As Integer
    Public Property name As String
    Public Property user_id As String
    Public Property is_main_contact As Boolean
    Public Property email_address As String

    Public Overridable Property contact_type As contact_type
    Public Overridable Property implementation As implementation

End Class

接下来的两个是E1和E3:

Imports System
Imports System.Collections.Generic

Partial Public Class contact_type
    Public Property contact_type_id As Integer
    Public Property contact_type_description As String

    Public Overridable Property contacts As ICollection(Of contact) = New HashSet(Of contact)

End Class
Imports System
Imports System.Collections.Generic

Partial Public Class implementation
    Public Property implementation_ID As Integer
    Public Property user_ID As String
    Public Property comments As String
    Public Property creation_date As Nullable(Of Date)
    Public Property customer_name As String
    Public Property customer_global_name As String
    Public Property customer_address As String
    Public Property customer_country As String
    Public Property service_order_number As String
    Public Property title As String
    Public Property contact_name As String

    Public Overridable Property Country As Country
    Public Overridable Property supportDetail As support_detail
    Public Overridable Property tradeCompliance As trade_compliance
    Public Overridable Property statusEffDates As ICollection(Of implementation_status_eff_date) = New HashSet(Of implementation_status_eff_date)
    Public Overridable Property hwsData As hws_data
    Public Overridable Property documents As ICollection(Of document) = New HashSet(Of document)
    Public Overridable Property contacts As ICollection(Of contact) = New HashSet(Of contact)

End Class

这是列表视图代码:

Imports System.Web.ModelBinding

Public Class ImplementationList
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Public Function GetImplementations(<QueryString("id")> categoryId As System.Nullable(Of Integer)) As IQueryable(Of implementation)
        Dim db = New RFI_SDM.RFI_DataEntities
        Dim query As IQueryable(Of implementation) = db.implementations
        Return query
    End Function
End Class

最后是RFI_DataEntities的代码:

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

Partial Public Class RFI_DataEntities
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=RFI_DataEntities")
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        Throw New UnintentionalCodeFirstException()
    End Sub

    Public Overridable Property Countries() As DbSet(Of Country)
    Public Overridable Property implementations() As DbSet(Of implementation)
    Public Overridable Property replacement_type() As DbSet(Of replacement_type)
    Public Overridable Property service_level_choice() As DbSet(Of service_level_choice)
    Public Overridable Property support_detail() As DbSet(Of support_detail)
    Public Overridable Property tat_end_choice() As DbSet(Of tat_end_choice)
    Public Overridable Property tat_measurement_basis() As DbSet(Of tat_measurement_basis)
    Public Overridable Property tat_start_choice() As DbSet(Of tat_start_choice)
    Public Overridable Property regions() As DbSet(Of region)
    Public Overridable Property contacts() As DbSet(Of contact)
    Public Overridable Property contact_type() As DbSet(Of contact_type)
    Public Overridable Property hws_data() As DbSet(Of hws_data)
    Public Overridable Property implementation_mgr() As DbSet(Of implementation_mgr)
    Public Overridable Property implementation_service_level() As DbSet(Of implementation_service_level)
    Public Overridable Property implementation_status() As DbSet(Of implementation_status)
    Public Overridable Property implementation_status_eff_date() As DbSet(Of implementation_status_eff_date)
    Public Overridable Property trade_compliance() As DbSet(Of trade_compliance)
    Public Overridable Property implementation_type() As DbSet(Of implementation_type)
    Public Overridable Property documents() As DbSet(Of document)
    Public Overridable Property document_type() As DbSet(Of document_type)
    Public Overridable Property LCM_Data() As DbSet(Of LCM_Data)
    Public Overridable Property LCM_document() As DbSet(Of LCM_document)

End Class

我得到的实际错误是:

指定的架构无效。错误: 关系&#39; RFI_DataModel.FK_contact_contact_type&#39;未加载,因为类型&#39; RFI_DataModel.contact&#39;不可用。

关系&#39; RFI_DataModel.FK_contact_implementation&#39;未加载,因为类型&#39; RFI_DataModel.contact&#39;不可用。

如果我删除了联系人&#39;来自contact_type和实现类的导航属性,列表视图加载没有问题。

0 个答案:

没有答案