Search code examples
vb.netentity-frameworkef-database-first

The entity type is not part of the model for the current context - DBFirst


I have a query which selects a list of subrep. The query looks like this:

Public Async Function ReturnSubRepsAsync(user As BaseUser) As Task(Of IList(Of SubRep)) Implements IGeneralAccountManagementRepository.ReturnSubRepsAsync
        'The entity type SubRep is not part of the model for the current context.
        Using webDataDBContext As New WebDataEntities()
            'So here it just has an SQL query which it will execute when requested
            Dim subReps = webDataDBContext.Subreps.Where(Function(x) x.CID = user.CID)

            If (String.IsNullOrEmpty(user.Password) = False) Then
                'Add another where onto the SQL query
                subReps = subReps.Where(Function(x) x.HASH_PASSWORD = user.PasswordHash)
            End If

            If (String.IsNullOrEmpty(user.Username) = False) Then
                'Add another where onto the SQL query
                subReps = subReps.Where(Function(x) x.username = user.Username)
            End If

            'Execute the query
            Return Await subReps.ToListAsync()
        End Using
    End Function

Now due to entity framework database not including validation such as MaxLength on strings, I decided to follow this tutorial: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation

And add another partial public class which uses a MetaDataType to implement the validation. Now during debugging, I thought it was an issue with a column so I decided to comment out all of the code inside my separate partial class to test if it works, so I'm not left with an empty husk of a partial class. It has not made a difference, it still errors on the first where of my query.

My entity framework generated class looks like this:

Partial Public Class Subrep
    Public Property id As Integer
    Public Property CID As String
    Public Property username As String
    Public Property LastName As String
    Public Property FirstName As String
    Public Property MIddleInitial As String
    Public Property Address As String
    Public Property Address2 As String
    Public Property City As String
    Public Property State As String
    Public Property Zip As String
    Public Property HomePhone As String
    Public Property EmailAddress As String
    Public Property InActive As String
    Public Property CellPhone As String
    Public Property StartDate As String
    Public Property AuditName As String
    Public Property Background As String
    Public Property FDCPA As String
    Public Property Choicepoint As String
    Public Property Badge As String
    Public Property ConfAgree As String
    Public Property NCCI_OK As String
    Public Property InactiveRepNo As String
    Public Property Approved As String
    Public Property DateApproved As String
    Public Property Denied As String
    Public Property DateDenied As String
    Public Property DeniedNotes As String
    Public Property DOB As String
    Public Property SSN As String
    Public Property MegansLaw As String
    Public Property MASTER As String
    Public Property VACATION As String
    Public Property FC_DEFAULT_FEE As Nullable(Of Decimal)
    Public Property LM_DEFAULT_FEE As Nullable(Of Decimal)
    Public Property MAS90 As String
    Public Property SOS As String
    Public Property HASH_PASSWORD As String
    Public Property changepass As String
    Public Property NOTIFICATION_OFF As String
    Public Property Capacity As String
    Public Property internalqc As String
    Public Property New_Rep As String
    Public Property Stand_By As String
    Public Property Vacation_Start_date As Nullable(Of Date)
    Public Property Vacation_End_date As Nullable(Of Date)
    Public Property sub_coreIP As String
    Public Property rating As String
    Public Property date_last_updated As Nullable(Of Date)

End Class

And my separate partial class which is supposed to be used for validation looks like this:

Partial Public Class SubRep
End Class

So even though it's empty I get the error listed in the title. I have gotten this to work with multiple entities perfectly fine.


Solution

  • Just wow.

    So with visual studio intellisense if you press F12 to try to find references it highlighted both partial classes Subrep and SubRep so that would lead you to believe that both were associated with one another. Turns out that because of the uppercase R in the second partial class SubRep, it's not associated with the model and therefore needs to match the case of the entity Subrep. Like this:

    Partial Public Class Subrep
    End Class
    

    What's even weirder is that the Property in my DBContext uses an uppercase SubRep. Like this:

    Public Overridable Property Subreps() As DbSet(Of SubRep)