entity-framework - EF4 POCO(不使用 T4): Mapping and metadata information could not be found for EntityType

标签 entity-framework .net-4.0 entity-framework-4

我有一个非常简单的控制台项目,其中包含一个实体模型(包含两个简单实体)、两个手工制作的 POCO 和一个手工制作的 Context 类。该程序会针对 DB 触发一个简单的查询,并且包括 LazyLoading 在内的所有内容都可以正常工作。

问题:一旦我添加了另一个实体数据模型(即使我添加了一个空模型),对 Ef2PlaygroundModel_3Container 中的 CreateObjectSet 的调用会引发以下异常:

Unhandled Exception: System.InvalidOperationException: Mapping and metadata information could not be found for EntityType 'EF2_Playground.Driver'.
  at System.Data.Objects.ObjectContext.GetTypeUsage(Type entityCLRType)
  at System.Data.Objects.ObjectContext.GetEntitySetFromContainer(EntityContainer container, Type entityCLRType, String exceptionParameterName)
  at System.Data.Objects.ObjectContext.GetEntitySetForType(Type entityCLRType, String exceptionParameterName)
  at System.Data.Objects.ObjectContext.CreateObjectSet[TEntity]()
  at EF2_Playground.Ef2PlaygroundModel_3Container.get_Drivers() in C:\...\Ef2PlaygroundModel_3Pocos.cs:line 64
  at EF2_Playground.Program.Main(String[] args) in C:\...\Program.cs:line 15

有没有人知道这里出了什么问题?

这是工作项目:

Ef2PlaygroundModel_3.edmx:

EF4 Data Model

代码生成策略设置为“无”
<?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="Ef2PlaygroundModel_3.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="Ef2PlaygroundModel_3StoreContainer">
          <EntitySet Name="Cars" EntityType="Ef2PlaygroundModel_3.Store.Cars" store:Type="Tables" Schema="dbo"/>
          <EntitySet Name="Drivers" EntityType="Ef2PlaygroundModel_3.Store.Drivers" store:Type="Tables" Schema="dbo"/>
          <EntitySet Name="CarDriver" EntityType="Ef2PlaygroundModel_3.Store.CarDriver" store:Type="Tables" Schema="dbo"/>
          <AssociationSet Name="FK_CarDriver_Car" Association="Ef2PlaygroundModel_3.Store.FK_CarDriver_Car">
            <End Role="Car" EntitySet="Cars"/>
            <End Role="CarDriver" EntitySet="CarDriver"/>
          </AssociationSet>
          <AssociationSet Name="FK_CarDriver_Driver" Association="Ef2PlaygroundModel_3.Store.FK_CarDriver_Driver">
            <End Role="Driver" EntitySet="Drivers"/>
            <End Role="CarDriver" EntitySet="CarDriver"/>
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Cars">
          <Key>
            <PropertyRef Name="Id"/>
          </Key>
          <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false"/>
          <Property Name="Brand" Type="nvarchar(max)" Nullable="false"/>
          <Property Name="Model" Type="nvarchar(max)" Nullable="false"/>
          <Property Name="ReleaseDate" Type="datetime" Nullable="true"/>
        </EntityType>
        <EntityType Name="Drivers">
          <Key>
            <PropertyRef Name="Id"/>
          </Key>
          <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false"/>
          <Property Name="Name" Type="nvarchar(max)" Nullable="false"/>
        </EntityType>
        <EntityType Name="CarDriver">
          <Key>
            <PropertyRef Name="Cars_Id"/>
            <PropertyRef Name="Drivers_Id"/>
          </Key>
          <Property Name="Cars_Id" Type="int" Nullable="false"/>
          <Property Name="Drivers_Id" Type="int" Nullable="false"/>
        </EntityType>
        <Association Name="FK_CarDriver_Car">
          <End Role="Car" Type="Ef2PlaygroundModel_3.Store.Cars" Multiplicity="1"/>
          <End Role="CarDriver" Type="Ef2PlaygroundModel_3.Store.CarDriver" Multiplicity="*"/>
          <ReferentialConstraint>
            <Principal Role="Car">
              <PropertyRef Name="Id"/>
            </Principal>
            <Dependent Role="CarDriver">
              <PropertyRef Name="Cars_Id"/>
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_CarDriver_Driver">
          <End Role="CarDriver" Type="Ef2PlaygroundModel_3.Store.CarDriver" Multiplicity="*"/>
          <End Role="Driver" Type="Ef2PlaygroundModel_3.Store.Drivers" Multiplicity="1"/>
          <ReferentialConstraint>
            <Principal Role="Driver">
              <PropertyRef Name="Id"/>
            </Principal>
            <Dependent Role="CarDriver">
              <PropertyRef Name="Drivers_Id"/>
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Ef2PlaygroundModel_3" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityContainer Name="Ef2PlaygroundModel_3Container" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="Cars" EntityType="Ef2PlaygroundModel_3.Car"/>
          <EntitySet Name="Drivers" EntityType="Ef2PlaygroundModel_3.Driver"/>
          <AssociationSet Name="CarDriver" Association="Ef2PlaygroundModel_3.CarDriver">
            <End Role="Car" EntitySet="Cars"/>
            <End Role="Driver" EntitySet="Drivers"/>
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Car">
          <Key>
            <PropertyRef Name="Id"/>
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity"/>
          <Property Type="String" Name="Brand" Nullable="false"/>
          <Property Type="String" Name="Model" Nullable="false"/>
          <Property Type="DateTime" Name="ReleaseDate" Nullable="true"/>
          <NavigationProperty Name="Drivers" Relationship="Ef2PlaygroundModel_3.CarDriver" FromRole="Car" ToRole="Driver"/>
        </EntityType>
        <EntityType Name="Driver">
          <Key>
            <PropertyRef Name="Id"/>
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity"/>
          <Property Type="String" Name="Name" Nullable="false"/>
          <NavigationProperty Name="Cars" Relationship="Ef2PlaygroundModel_3.CarDriver" FromRole="Driver" ToRole="Car"/>
        </EntityType>
        <Association Name="CarDriver">
          <End Type="Ef2PlaygroundModel_3.Car" Role="Car" Multiplicity="*"/>
          <End Type="Ef2PlaygroundModel_3.Driver" Role="Driver" Multiplicity="*"/>
        </Association>
      </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="Ef2PlaygroundModel_3StoreContainer" CdmEntityContainer="Ef2PlaygroundModel_3Container">
          <EntitySetMapping Name="Cars">
            <EntityTypeMapping TypeName="IsTypeOf(Ef2PlaygroundModel_3.Car)">
              <MappingFragment StoreEntitySet="Cars">
                <ScalarProperty Name="Id" ColumnName="Id"/>
                <ScalarProperty Name="Brand" ColumnName="Brand"/>
                <ScalarProperty Name="Model" ColumnName="Model"/>
                <ScalarProperty Name="ReleaseDate" ColumnName="ReleaseDate"/>
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Drivers">
            <EntityTypeMapping TypeName="IsTypeOf(Ef2PlaygroundModel_3.Driver)">
              <MappingFragment StoreEntitySet="Drivers">
                <ScalarProperty Name="Id" ColumnName="Id"/>
                <ScalarProperty Name="Name" ColumnName="Name"/>
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <AssociationSetMapping Name="CarDriver" TypeName="Ef2PlaygroundModel_3.CarDriver" StoreEntitySet="CarDriver">
            <EndProperty Name="Car">
              <ScalarProperty Name="Id" ColumnName="Cars_Id"/>
            </EndProperty>
            <EndProperty Name="Driver">
              <ScalarProperty Name="Id" ColumnName="Drivers_Id"/>
            </EndProperty>
          </AssociationSetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly"/>
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true"/>
        <DesignerProperty Name="EnablePluralization" Value="False"/>
        <DesignerProperty Name="CodeGenerationStrategy" Value="None"/>
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="Ef2PlaygroundModel_3">
        <EntityTypeShape EntityType="Ef2PlaygroundModel_3.Car" Width="1.5" PointX="3.25" PointY="1.625" Height="1.787985026041667"/>
        <EntityTypeShape EntityType="Ef2PlaygroundModel_3.Driver" Width="1.5" PointX="5.375" PointY="1.625" Height="1.59568359375"/>
        <AssociationConnector Association="Ef2PlaygroundModel_3.CarDriver">
          <ConnectorPoint PointX="4.75" PointY="2.422841796875"/>
          <ConnectorPoint PointX="5.375" PointY="2.422841796875"/>
        </AssociationConnector>
      </Diagram>
    </edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>

app.config:
<configuration>
  <connectionStrings>
    <add 
      name="Ef2PlaygroundModel_3Container" 
      connectionString="metadata=res://*/Ef2PlaygroundModel_3.csdl|res://*/Ef2PlaygroundModel_3.ssdl|res://*/Ef2PlaygroundModel_3.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SqlExpress;Initial Catalog=Ef2PlaygroundModel_3;Integrated Security=True;MultipleActiveResultSets=True&quot;" 
      providerName="System.Data.EntityClient" 
    />
  </connectionStrings>
</configuration>

Ef2PlaygroundModel_3Pocos.cs:
using System;
using System.Collections.Generic;
using System.Data.Objects;

namespace EF2_Playground
{
  public class Car
  {
    public Car()
    {
      Drivers = new List<Driver>();
    }

    public int Id { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }
    public DateTime? ReleaseDate { get; set; }

    public virtual List<Driver> Drivers { get; private set; }
  }

  public class Driver
  {
    public Driver()
    {
      Cars = new List<Car>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual List<Car> Cars { get; private set; }
  }

  public class Ef2PlaygroundModel_3Container : ObjectContext
  { 
    public Ef2PlaygroundModel_3Container()
      : base("name=Ef2PlaygroundModel_3Container") 
    {
      ContextOptions.LazyLoadingEnabled = true;
    }

    public IObjectSet<Car> Cars
    {
      get { return CreateObjectSet<Car>(); }
    }

    public IObjectSet<Driver> Drivers
    {
      get { return CreateObjectSet<Driver>(); }
    }
  }
}

程序.cs:
using System;

namespace EF2_Playground
{
  class Program
  {
    static void Main(string[] args)
    {
      using (var ctx = new Ef2PlaygroundModel_3Container())
      {
        foreach (var driver in ctx.Drivers)
        {
          Console.WriteLine(driver.Name);
          foreach (var car in driver.Cars)
          {
            Console.WriteLine("   drives a {0} - {1} (released on {2})", car.Brand, car.Model, car.ReleaseDate);
          }
        }
      }
    }
  }
}

最后 Model1.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 xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" Namespace="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005">
        <EntityContainer Name="Model1TargetContainer">
        </EntityContainer>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true">
        </EntityContainer>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs" Space="C-S">
        <Alias Key="Model" Value="Model1"/>
        <Alias Key="Target" Value="Model1.Store"/>
        <EntityContainerMapping CdmEntityContainer="Model1Container" StorageEntityContainer="Model1TargetContainer">
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly"/>
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true"/>
        <DesignerProperty Name="EnablePluralization" Value="False"/>
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="Model1"/>
    </edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>

最佳答案

好的,我想我明白了。我将第二个模型(阻碍项目的模型)简化为以下内容:

Class1.cs

using System.Data.Objects.DataClasses;
[assembly: EdmSchemaAttribute()]

砰!出现了众所周知的异常。

在许多情况下,阅读 documentation 有助于:

Mapping POCO entities is not supported if any mapping attributes are applied to custom data classes, including EdmSchemaAttribute at the assembly level.



当然,我不会从字面上将映射属性添加到 CUSTOM 数据类,但这对 EdmSchemaAttribute 并不重要,因为它存在于程序集级别。

添加第二个非 POCO 模型会导致生成包含(至少)EdmSchemaAttribute 且不受支持的类的代码生成。

我学到了什么:不要在一个组件中混合 POCO 和非 POCO 模型。

关于entity-framework - EF4 POCO(不使用 T4): Mapping and metadata information could not be found for EntityType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2306940/

相关文章:

c# - 如何抑制由我​​无法更改的代码显示的对话框?

c# - Entity Framework : Set nullable to false, 但类型仍可为空

c# - 从 Entity Framework 中仅检索基类

c# - Entity Framework 返回 NULL 链接对象 C#

c# - Entity Framework ,获取子类的类型?

c# - Entity Framework 仅适用于Web应用程序吗?

.net - 为什么 .NET 4 中的类不是协变的?

c# - 使用 Entity Framework 分组

c# - 为什么 [Enum].Parse 有一个 ignoreCase 参数?

entity-framework - Entity Framework 4/POCO - 从哪里开始?