实体类型不是当前上下文模型的一部分 - DBFirst

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

我有一个选择 subrep 列表的查询。查询如下所示:

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

现在由于 entity framework 数据库不包括 MaxLength 字符串验证,我决定按照本教程进行操作:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation

并添加另一个部分 public class,它使用 MetaDataType 来实现验证。现在在调试期间,我认为这是一个列的问题,所以我决定注释掉我单独的部分 class 中的所有代码以测试它是否有效,所以我不会留下一个空壳部分 class。它没有什么不同,它仍然在我查询的第一个位置出错。

我的 entity framework 生成的 class 看起来像这样:

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

我单独的部分 class 应该用于验证,如下所示:

Partial Public Class SubRep
End Class

所以即使它是空的,我也会得到标题中列出的错误。我已经让它可以完美地与多个实体一起工作。

哇哦。

因此,使用 visual studio 智能感知,如果您按 F12 尝试查找引用,它会突出显示部分 classes SubrepSubRep,这样会让您相信两者相互关联。事实证明,由于第二部分 class SubRep 中的大写 R 与模型无关,因此需要匹配实体 Subrep 的大小写。像这样:

Partial Public Class Subrep
End Class

更奇怪的是,我的 DBContext 中的 属性 使用大写字母 SubRep。像这样:

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