实体类型 <class> 不是当前上下文模型的一部分

Posted

技术标签:

【中文标题】实体类型 <class> 不是当前上下文模型的一部分【英文标题】:The entity type <class> is not part of the model for the current context 【发布时间】:2012-12-13 16:23:47 【问题描述】:

这是模型优先的方法。我已经对此进行了广泛的研究,但没有提出答案。我已经尝试了以下链接中的所有建议:

这似乎是同样的问题,但没有解决办法 The entity type <classname> is not part of the model for the current context

这些是我已经研究过的链接,所以请不要重复回答---- EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context The entity type is not part of the model, EF 5

这是 EDMX(为简洁起见):

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Insight_Model.Store" Alias="Self" Provider="System.Data.SqlClient"    ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
    <EntityContainer Name="Insight_ModelStoreContainer">
      <EntitySet Name="vwCompanyHeader" EntityType="Insight_Model.Store.vwCompanyHeader" store:Type="Views" store:Schema="dbo" store:Name="vwCompanyHeader">
        <DefiningQuery>SELECT 
  [vwCompanyHeader].[ID] AS [ID], 
  [vwCompanyHeader].[Name] AS [Name], 
  [vwCompanyHeader].[Active] AS [Active], 
  [vwCompanyHeader].[Creator] AS [Creator], 
  [vwCompanyHeader].[Created] AS [Created], 
  [vwCompanyHeader].[Modifier] AS [Modifier], 
  [vwCompanyHeader].[Modified] AS [Modified]
  FROM [dbo].[vwCompanyHeader] AS [vwCompanyHeader]</DefiningQuery>
      </EntitySet>
      <EntitySet Name="vwWorkOrderHeader" EntityType="Insight_Model.Store.vwWorkOrderHeader" store:Type="Views" store:Schema="dbo" store:Name="vwWorkOrderHeader">
        <DefiningQuery>SELECT 
  [vwWorkOrderHeader].[ID] AS [ID], 
  [vwWorkOrderHeader].[Active] AS [Active], 
  [vwWorkOrderHeader].[Name] AS [Name], 
  [vwWorkOrderHeader].[Creator] AS [Creator], 
  [vwWorkOrderHeader].[Created] AS [Created], 
  [vwWorkOrderHeader].[Modifier] AS [Modifier], 
  [vwWorkOrderHeader].[Modified] AS [Modified], 
  [vwWorkOrderHeader].[Leader] AS [Leader], 
  [vwWorkOrderHeader].[LeadTakenDate] AS [LeadTakenDate], 
  [vwWorkOrderHeader].[ProjectID] AS [ProjectID], 
  [vwWorkOrderHeader].[ProjectName] AS [ProjectName], 
  [vwWorkOrderHeader].[WorkOrderStatus] AS [WorkOrderStatus], 
  [vwWorkOrderHeader].[WorkOrderSubStatus] AS [WorkOrderSubStatus], 
  [vwWorkOrderHeader].[WorkOrderType] AS [WorkOrderType], 
  [vwWorkOrderHeader].[WorkOrderSubType] AS [WorkOrderSubType], 
  [vwWorkOrderHeader].[WorkOrderPriority] AS [WorkOrderPriority], 
  [vwWorkOrderHeader].[RefNumber] AS [RefNumber], 
  [vwWorkOrderHeader].[Request] AS [Request]
  FROM [dbo].[vwWorkOrderHeader] AS [vwWorkOrderHeader]</DefiningQuery>
      </EntitySet>
    </EntityContainer>
    <EntityType Name="vwCompanyHeader">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Name="ID" Type="int" Nullable="false" />
      <Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
      <Property Name="Active" Type="bit" Nullable="false" />
      <Property Name="Creator" Type="varchar" Nullable="false" MaxLength="51" />
      <Property Name="Created" Type="date" Nullable="false" />
      <Property Name="Modifier" Type="varchar" Nullable="false" MaxLength="51" />
      <Property Name="Modified" Type="date" Nullable="false" />
    </EntityType>
    <EntityType Name="vwWorkOrderHeader">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Name="ID" Type="int" Nullable="false" />
      <Property Name="Active" Type="bit" Nullable="false" />
      <Property Name="Name" Type="varchar" Nullable="false" MaxLength="15" />
      <Property Name="Creator" Type="varchar" Nullable="false" MaxLength="51" />
      <Property Name="Created" Type="date" Nullable="false" />
      <Property Name="Modifier" Type="varchar" Nullable="false" MaxLength="51" />
      <Property Name="Modified" Type="date" Nullable="false" />
      <Property Name="Leader" Type="varchar" Nullable="false" MaxLength="51" />
      <Property Name="LeadTakenDate" Type="date" Nullable="false" />
      <Property Name="ProjectID" Type="int" Nullable="false" />
      <Property Name="ProjectName" Type="varchar" Nullable="false" MaxLength="15" />
      <Property Name="WorkOrderStatus" Type="varchar" Nullable="false" MaxLength="35" />
      <Property Name="WorkOrderSubStatus" Type="varchar" Nullable="false" MaxLength="35" />
      <Property Name="WorkOrderType" Type="varchar" Nullable="false" MaxLength="35" />
      <Property Name="WorkOrderSubType" Type="varchar" Nullable="false" MaxLength="35" />
      <Property Name="WorkOrderPriority" Type="varchar" Nullable="false" MaxLength="35" />
      <Property Name="RefNumber" Type="varchar" Nullable="false" MaxLength="25" />
      <Property Name="Request" Type="varchar" Nullable="false" MaxLength="1500" />
    </EntityType>
  </Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
  <Schema Namespace="Insight_Model" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
    <EntityContainer Name="Insight_Entities" annotation:LazyLoadingEnabled="false">
      <EntitySet Name="CompanyHeaders" EntityType="Insight_Model.CompanyHeader" />
      <EntitySet Name="WorkOrderHeaders" EntityType="Insight_Model.WorkOrderHeader" />
    </EntityContainer>
    <EntityType Name="CompanyHeader">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Type="String" Name="Name" MaxLength="50" FixedLength="false" Unicode="false" Nullable="false" />
      <Property Type="Boolean" Name="Active" Nullable="false" />
      <Property Type="String" Name="Creator" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
      <Property Type="DateTime" Name="Created" Nullable="false" />
      <Property Type="String" Name="Modifier" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
      <Property Type="DateTime" Name="Modified" Nullable="false" />
      <Property Type="Int32" Name="ID" Nullable="false" />
    </EntityType>
    <EntityType Name="WorkOrderHeader">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Type="Int32" Name="ID" Nullable="false" />
      <Property Type="Boolean" Name="Active" Nullable="false" />
      <Property Type="String" Name="Name" Nullable="false" MaxLength="15" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="Creator" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
      <Property Type="DateTime" Name="Created" Nullable="false" />
      <Property Type="String" Name="Modifier" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
      <Property Type="DateTime" Name="Modified" Nullable="false" />
      <Property Type="String" Name="Leader" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
      <Property Type="DateTime" Name="LeadTakenDate" Nullable="false" />
      <Property Type="Int32" Name="ProjectID" Nullable="false" />
      <Property Type="String" Name="ProjectName" Nullable="false" MaxLength="15" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="WorkOrderStatus" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="WorkOrderSubStatus" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="WorkOrderType" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="WorkOrderSubType" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="WorkOrderPriority" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="RefNumber" MaxLength="25" Nullable="false" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="Request" Nullable="false" MaxLength="1500" FixedLength="false" Unicode="false" />
    </EntityType>
    </Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
  <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
    <EntityContainerMapping StorageEntityContainer="Insight_ModelStoreContainer" CdmEntityContainer="Insight_Entities">
      <EntitySetMapping Name="CompanyHeaders">
        <EntityTypeMapping TypeName="Insight_Model.CompanyHeader">
          <MappingFragment StoreEntitySet="vwCompanyHeader">
            <ScalarProperty Name="ID" ColumnName="ID" />
            <ScalarProperty Name="Modified" ColumnName="Modified" />
            <ScalarProperty Name="Modifier" ColumnName="Modifier" />
            <ScalarProperty Name="Created" ColumnName="Created" />
            <ScalarProperty Name="Creator" ColumnName="Creator" />
            <ScalarProperty Name="Active" ColumnName="Active" />
            <ScalarProperty Name="Name" ColumnName="Name" />
          </MappingFragment>
        </EntityTypeMapping>
      </EntitySetMapping>
      <EntitySetMapping Name="WorkOrderHeaders">
        <EntityTypeMapping TypeName="Insight_Model.WorkOrderHeader">
          <MappingFragment StoreEntitySet="vwWorkOrderHeader">
            <ScalarProperty Name="Request" ColumnName="Request" />
            <ScalarProperty Name="RefNumber" ColumnName="RefNumber" />
            <ScalarProperty Name="WorkOrderPriority" ColumnName="WorkOrderPriority" />
            <ScalarProperty Name="WorkOrderSubType" ColumnName="WorkOrderSubType" />
            <ScalarProperty Name="WorkOrderType" ColumnName="WorkOrderType" />
            <ScalarProperty Name="WorkOrderSubStatus" ColumnName="WorkOrderSubStatus" />
            <ScalarProperty Name="WorkOrderStatus" ColumnName="WorkOrderStatus" />
            <ScalarProperty Name="ProjectName" ColumnName="ProjectName" />
            <ScalarProperty Name="ProjectID" ColumnName="ProjectID" />
            <ScalarProperty Name="LeadTakenDate" ColumnName="LeadTakenDate" />
            <ScalarProperty Name="Leader" ColumnName="Leader" />
            <ScalarProperty Name="Modified" ColumnName="Modified" />
            <ScalarProperty Name="Modifier" ColumnName="Modifier" />
            <ScalarProperty Name="Created" ColumnName="Created" />
            <ScalarProperty Name="Creator" ColumnName="Creator" />
            <ScalarProperty Name="Name" ColumnName="Name" />
            <ScalarProperty Name="Active" ColumnName="Active" />
            <ScalarProperty Name="ID" ColumnName="ID" />
          </MappingFragment>
        </EntityTypeMapping>
      </EntitySetMapping>
      </EntityContainerMapping>
  </Mapping>
</edmx:Mappings>

存储库代码:

Imports System.Linq.Expressions
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity

Public Class Repository(Of T As Class, IEntity)
Implements IRepository(Of T)

#Region "Attributes"
Private objectContext As IDatabaseContext
Protected objectSet As IDbSet(Of T)
#End Region

Public Sub New(objContext As IDatabaseContext)
    objectContext = objContext
    objectSet = objectContext.Set(Of T)()
End Sub
Public Function FindAll() As IQueryable(Of T) Implements IRepository(Of T).FindAll
    Return objectSet
End Function

Public Function FindWhere(predicate As Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements Interfaces.IRepository(Of T).FindWhere
    Return objectSet.Where(predicate)
End Function

Public Function FindById(id As Integer) As T Implements IRepository(Of T).FindById
    Return objectSet.Single(Function(o) o.ID = id)
End Function

Public Sub Add(newEntity As T) Implements IRepository(Of T).Add
    objectSet.Add(newEntity)
End Sub

Public Sub Remove(entity As T) Implements IRepository(Of T).Remove
    objectSet.Remove(entity)
End Sub

Public Function Count(Optional predicate As Expression(Of Func(Of T, Boolean)) = Nothing) As Integer Implements IRepository(Of T).Count
    Return If((predicate Is Nothing), objectSet.Count, objectSet.Where(predicate).Count)
End Function

Public Function Exist(Optional predicate As Expression(Of Func(Of T, Boolean)) = Nothing) As Boolean Implements IRepository(Of T).Exist
    Return If((predicate Is Nothing), objectSet.Any, objectSet.Where(predicate).Any)
End Function
End Class

DatabaseContext 代码:

Imports System.Data.Entity
Imports Insight.DataLayer.Interfaces
Imports System.Data.Objects
Imports System.Configuration

Public Class DatabaseContext
Inherits DbContext
Implements IDatabaseContext

Const defaultConnectStringName = "Insight_Entities"

Public Sub New()
     Me.New(ConfigurationManager.ConnectionStrings(defaultConnectStringName).ConnectionString)
End Sub
Public Sub New(connectionString As String)
    MyBase.New(connectionString)
    MyBase.Configuration.LazyLoadingEnabled = False
    MyBase.Configuration.ProxyCreationEnabled = False
End Sub
Public Overloads Sub SaveChanges() Implements Interfaces.IDatabaseContext.SaveChanges
    MyBase.SaveChanges()
End Sub

Public Overloads Function [Set](Of T As Class)() As System.Data.Entity.IDbSet(Of T) Implements Interfaces.IDatabaseContext.Set
    Return MyBase.Set(Of T)()
End Function
End Class

还有 2 个类文件:

WorkOrderHeader:

Imports System.ComponentModel.DataAnnotations
Imports System.Collections.ObjectModel
Imports System.Runtime.Serialization
Imports System.Data.Services.Common

Partial Public Class WorkOrderHeader
Inherits DataTransferObjectHeaderBase

Public Sub New()

End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime, sLeader As String, dtLeadTaken As Date, iProjectID As Integer, sProjectName As String, sWorkOrderStatus As String, sWorkOrderSubStatus As String, sWorkOrderType As String, sWorkOrderSubType As String, sWorkOrderPriority As String, sRequest As String)
    Me.New(iID, sName, bActive, sCreator, dtCreated, sModifier, dtModified, sLeader, dtLeadTaken, iProjectID, sProjectName, sWorkOrderStatus, sWorkOrderSubStatus, sWorkOrderType, sWorkOrderSubType, sWorkOrderPriority, "", sRequest)
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime, sLeader As String, dtLeadTaken As Date, iProjectID As Integer, sProjectName As String, sWorkOrderStatus As String, sWorkOrderSubStatus As String, sWorkOrderType As String, sWorkOrderSubType As String, sWorkOrderPriority As String, sRefNumber As String, sRequest As String)
    ID = iID
    Name = sName
    Active = bActive
    Creator = sCreator
    Created = dtCreated
    Modifier = sModifier
    Modified = dtModified
    Leader = sLeader
    LeadTaken = dtLeadTaken
    ProjectID = iProjectID
    ProjectName = sProjectName
    WorkOrderStatus = sWorkOrderStatus
    WorkOrderSubStatus = sWorkOrderSubStatus
    WorkOrderType = sWorkOrderType
    WorkOrderSubType = sWorkOrderSubType
    WorkOrderPriority = sWorkOrderPriority
    RefNumber = sRefNumber
    Request = sRequest
End Sub

<DataMember()> _
Public Property Leader() As String
    Get
        Return _leader
    End Get
    Set(value As String)
        _leader = value
    End Set
End Property
Private _leader As String

<DataMember()> _
Public Property LeadTaken() As Date
    Get
        Return _leadTaken
    End Get
    Set(value As Date)
        _leadTaken = value
    End Set
End Property
Private _leadTaken As Date

<DataMember()> _
Public Property ProjectID() As Int32
    Get
        Return _projectID
    End Get
    Set(value As Int32)
        If (_projectID <> value) Then
            _projectID = value
        End If
    End Set
End Property
Private _projectID As Int32

<DataMember()> _
Public Property ProjectName() As String
    Get
        Return _projectName
    End Get
    Set(value As String)
        If (_projectName <> value) Then
            _projectName = value
        End If
    End Set
End Property
Private _projectName As String

<DataMember()> _
Public Property WorkOrderStatus() As String
    Get
        Return _workOrderStatus
    End Get
    Set(value As String)
        _workOrderStatus = value
    End Set
End Property
Private _workOrderStatus As String

<DataMember()> _
Public Property WorkOrderSubStatus() As String
    Get
        Return _workOrderSubStatus
    End Get
    Set(value As String)
        _workOrderSubStatus = value
    End Set
End Property
Private _workOrderSubStatus As String

<DataMember()> _
Public Property WorkOrderType() As String
    Get
        Return _workOrderType
    End Get
    Set(value As String)
        _workOrderType = value
    End Set
End Property
Private _workOrderType As String

<DataMember()> _
Public Property WorkOrderSubType() As String
    Get
        Return _workOrderSubType
    End Get
    Set(value As String)
        _workOrderSubType = value
    End Set
End Property
Private _workOrderSubType As String

<DataMember()> _
Public Property WorkOrderPriority() As String
    Get
        Return _workOrderPriority
    End Get
    Set(value As String)
        _workOrderPriority = value
    End Set
End Property
Private _workOrderPriority As String

<DataMember()> _
Public Property RefNumber() As String
    Get
        Return _refNumber
    End Get
    Set(value As String)
        _refNumber = value
    End Set
End Property
Private _refNumber As String

<DataMember()> _
Public Property Request() As String
    Get
        Return _request
    End Get
    Set(value As String)
        _request = value
    End Set
End Property
Private _request As String

End Class
#End Region

公司标题:

Imports System.ComponentModel.DataAnnotations
Imports System.Collections.ObjectModel
Imports System.Runtime.Serialization
Imports System.Data.Services.Common


<DataServiceKey("ID")> _
<MetadataTypeAttribute(GetType(CompanyHeader.CompanyHeaderMetadata))> _
Partial Public Class CompanyHeader
Inherits DataTransferObjectHeaderBase

Friend NotInheritable Class CompanyHeaderMetadata

    'Metadata classes are not meant to be instantiated.
    Private Sub New()
        MyBase.New()
    End Sub

    <DataMember()> _
    Public Property Active As Boolean

    <DataMember()> _
    Public Property ID As Integer

    <DataMember()> _
    Public Property Created As DateTime

    <DataMember()> _
    Public Property Creator As String

    <DataMember()> _
    Public Property Modified As DateTime

    <DataMember()> _
    Public Property Modifier As String

    <DataMember()> _
    Public Property Name As String
End Class

Public Sub New()

End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime)
    ID = iID
    Name = sName
    Active = bActive
    Creator = sCreator
    Created = dtCreated
    Modifier = sModifier
    Modified = dtModified
End Sub
End Class

单元测试代码:

<TestMethod()> _
Public Sub WorkOrderServiceTests_TestConnectivityThruService5()
    Dim woService As New DatabaseContext
    Dim woHeaders = New Repository(Of WorkOrderHeader)(woService)
    Dim woTests = New Repository(Of WorkOrderTest)(woService)
    Dim coHeaders = New Repository(Of CompanyHeader)(woService)
    Assert.IsTrue(woHeaders.Count <> 0)
End Sub

我尝试解决的问题:

1) 检查数据库 - 两个查询都正确返回数据。 从 vwWorkOrderHeader 中选择 * 从 vwCompanyHeader 中选择 *

2) 从 EDMX 中删除 WorkOrderHeader,然后从数据库中重新创建它。

3) 最小化 DBContext 以确保它不是服务查询在做一些有趣的事情。这正确地返回了对象,所以这意味着它是上下文级别或更低级别的东西。

4) 逐步完成这两个测试,以确保它们使用相同的方法来构建上下文 - 你这样做。

4) 逐步完成这两个测试,以确保它们使用相同的方法来构建存储库 - 你这样做。

5) 唯一的区别是在DatabaseContext的以下函数期间:

Public Overloads Function [Set](Of T As Class)() As System.Data.Entity.IDbSet(Of T) Implements Interfaces.IDatabaseContext.Set
    Return MyBase.Set(Of T)()
End Function

WorkOrderHeader 代码在 MyBase.Set(Of T)() 的手表中具有以下内容

System.Data.Entity.DbSet(Of Insight.Model.WorkOrderHeader) System.Data.Entity.DbSet(Of Insight.Model.WorkOrderHeader)

CompanyHeader 代码在 MyBase.Set(Of T)() 的手表中具有以下内容

SELECT   [Extent1].[Name] AS [Name],   [Extent1].[Active] AS [Active],   [Extent1].[Creator] AS [Creator],   [Extent1].[Created] AS [Created],   [Extent1].[Modifier] AS [Modifier],   [Extent1].[Modified] AS [Modified],   [Extent1].[ID] AS [ID]  FROM (SELECT        [vwCompanyHeader].[ID] AS [ID],        [vwCompanyHeader].[Name] AS [Name],        [vwCompanyHeader].[Active] AS [Active],        [vwCompanyHeader].[Creator] AS [Creator],        [vwCompanyHeader].[Created] AS [Created],        [vwCompanyHeader].[Modifier] AS [Modifier],        [vwCompanyHeader].[Modified] AS [Modified]       FROM [dbo].[vwCompanyHeader] AS [vwCompanyHeader]) AS [Extent1]   System.Data.Entity.DbSet(Of Insight.Model.CompanyHeader)

我对此完全不知所措,并且正在进入解决同一问题的第三天。我希望对这个问题有新的认识可能会发现我犯的错误,或者指出我没有想到的测试问题的方向。

更新:

我删除了服务代码以确保它在 DBContext 中。我已将服务从等式中分离出来,以便仅使用存储库并且它们都使用相同的 DatabaseContext,因此它与连接字符串无关。我验证了模型浏览器中 Insight_Model/EntityContainer:Insight_Entities/Entity Sets/WorkOrderHeaders 下存在,并且 Insight_Model/Entity Types/WorkOrderHeader 下存在。似乎 DatabaseContext 甚至没有尝试查询实体集的概念模型。

【问题讨论】:

【参考方案1】:

3 (4) 天后(取决于您对日子的看法,我会根据我睡觉的时间来区分日子,而不一定是太阳下山/升起的时间),我找到了答案。在上面的 EDMX 文件中,我有一个名为 LeadTakenDate 的列,在 WorkOrderHeader 类中,我使用了字段名称 LeadTaken。这对我来说是一个愚蠢的错误。

这对您有什么好处?以下是我解决问题的方法:

首先,我通过 MOQing 零件将问题精确地隔离到一个区域,以查看问题在兔子洞中的深度。一旦我发现问题出在 DatabaseContext 中,我就编写了一个测试用例,创建了一个工作用例和一个非工作用例。这隔离了问题,让我能够测试理论以更快地解决它。在尝试使用 EF 5.x 持久性无知 poco 生成器之前,我尝试了一千种不同的方法。我在 WorkOrderHeader 类中注释掉了我的所有代码并将生成的代码复制进去。重新运行我的测试并且它们起作用了,所以我开始取消注释我的属性并注释掉生成的代码,然后重新运行我的测试,直到我找到适当的属性。

错误消息非常神秘,很难排除故障。

【讨论】:

Aaron 完成了如此冗长而详细的问题、编辑、更新和回答。对不起,没有其他人打扰你标记你。我做到了! 为了你们的努力和社区关怀,我刚刚给你们两个加分了!干杯,继续努力! 我犯了完全相同的错误,我花了两个小时弄清楚,最后当我读到你的前三行时,我想等一下,让我看看我的 poco 课程,然后你就走了.. .我看到了罪魁祸首...我将 poco 课程设置为Id,在我的数据库中我更改为CampusId,这让我非常失望,我已经知道我的错误... 那么在您的情况下,问题是属性名称不正确?如果是这种情况,那么此错误消息非常具有误导性... :( 我遇到了同样的错误,无法找到此错误消息背后的内容...

以上是关于实体类型 <class> 不是当前上下文模型的一部分的主要内容,如果未能解决你的问题,请参考以下文章

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

为啥我收到错误实体类型 LAB_INVOICE_VIEW 不是当前上下文模型的一部分。?

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

实体类型 IdentityRole 不是当前上下文 Asp.net mvc 模型的一部分

实体类型 ApplicationUser 不是当前上下文模型的一部分。在项目开始时使用了两个不同的数据库

实体类型 不是当前背景模型的一部分