8

I an using EF6 database first design in a web application. There is one entity that is involved in two 1-to-many relationships (the E2 in this example):
E1 <--- E2 ---> E3

E1 and E3 both have a navigation property for E2 (called E2s). When I try to ListView of E3, I get the error listed in the title. If I remove the navigation property for E2 from E3, everything works just fine! I've tried looking all over google to no avail. Any help/direction would be appreciated.

Added on 5/15:

tmg - hope this is what you are wanting.

This would be the 'E2' in my example:

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

The next two are the E1 and 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

This is the list view code:

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

And finally the code for 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

The actual error I'm getting is:

Schema specified is not valid. Errors: The relationship 'RFI_DataModel.FK_contact_contact_type' was not loaded because the type 'RFI_DataModel.contact' is not available.

The relationship 'RFI_DataModel.FK_contact_implementation' was not loaded because the type 'RFI_DataModel.contact' is not available.

If I remove the 'contact' navigation property from the contact_type and implementation classes, the list view loads without issue.

Andrew
  • 81
  • 3
  • Did EF generate all of the relationships from your database? Or did you manually add any part of this relationship? (And if you did, what parts did you manually add?) – Vaccano May 14 '15 at 21:22
  • It was generated from my database. Right now I have the relationship/FK in place in the ER diagram, but deleted the related navigation property from E1 and E3 and I get the list of E3. Since this is just in development, the table that E2 is based on is empty, but that should not matter I would hope. It isn't expected to have data all the time is it?? How would that work for adding new E1s and E3s? – Andrew May 14 '15 at 21:35
  • Can you show how you are getting the data from your dbContext and the ListView? – tmg May 15 '15 at 06:22
  • oh...don't know if this matters, but there are no contacts at this time, the table is empty. i would think that the EF would be able to deal with this, but maybe not. – Andrew May 15 '15 at 20:55
  • From what I see everything seems normal. Maybe you can check the edmx file (the XML) and check if the contact type is in the `` node as `EntityType`. Maybe you should delete and re-add `Contact`. – Gert Arnold May 16 '15 at 10:02
  • BTW is it SQL Server in your case? Have you tried to refresh or delete the model and add it again? – Orif Khodjaev May 16 '15 at 17:09
  • Orif, I have updated the three tables involved many times, are you talking about refreshing the entire model? Gert, yes, contact_type is listed in the edmx as an EntityType. It is listed after 'implementation' EntityType, if that matters. Will try deleting and re-adding contact and see what happens. will let you know if that works. – Andrew May 18 '15 at 14:14
  • If i'm correct, I would also make sure the E1 table and E3 table are not filled up with anything yet, besause if these table are searching for there contact in E2 and there is nothing in E2, then the foreign key in E1 and E3 cannot be found in E2... witch is a source of errors to me – Antoine Pelletier Oct 06 '15 at 15:14
  • Been a while since I've worked with EF6, but I was thinking maybe the contact navigation properties need to be virtual, so that you can lazy load them as needed. Also, You may need to employ some fluent api mappings manually in your context constructor if you have many to many mappings (EF assumes 1 to many when it does it for you). – DukeDidntNukeEm Aug 03 '17 at 17:59

0 Answers0