EF4 POCO(不使用T4):映射和元数​​据信息无法找到的EntityType [英] EF4 POCO (not using T4): Mapping and metadata information could not be found for EntityType

查看:389
本文介绍了EF4 POCO(不使用T4):映射和元数​​据信息无法找到的EntityType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty的与实体模型(包含两个简单的实体),两个手工波苏斯和一个手工制作的Context类简单的控制台项目。该方案将触发对数据库的简单查询和一切,包括LazyLoading工作正常。

的问题:的,只要我再添实体数据模型(即使我添加空单),在Ef2PlaygroundModel_3Container来电来CreateObjectSet抛出以下异常:

 未处理的异常:System.InvalidOperationException:映射和元数​​据信息无法发现的EntityTypeEF2_Playground.Driver。
  在System.Data.Objects.ObjectContext.GetTypeUsage(类型entityCLRType)
  在System.Data.Objects.ObjectContext.GetEntitySetFromContainer(集装箱EntityContainer相关,类型entityCLRType,字符串exceptionParameterName)
  在System.Data.Objects.ObjectContext.GetEntitySetForType(类型entityCLRType,字符串exceptionParameterName)
  在System.Data.Objects.ObjectContext.CreateObjectSet [TEntity]()
  在EF2_Playground.Ef2PlaygroundModel_3Container.get_Drivers()在C:\ ... \ Ef2PlaygroundModel_3Pocos.cs:行64
  在EF2_Playground.Program.Main(字串[] args)在C:\ ... \的Program.cs:行15
 

有没有人有什么错误在这里的想法?


这是工作项目:

Ef2PlaygroundModel_3.edmx:

EF4数据模型

code生成策略设置为无

 < XML版本=1.0编码=UTF-8&GT?;
< EDMX:EDMX版本=2.0的xmlns:EDMX =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
  <! -  EF运行时的内容 - >
  < EDMX:运行>
    <! -  SSDL内容 - >
    < EDMX:StorageModels>
      <模式命名空间=Ef2PlaygroundModel_3.Store别名=自我提供程序=System.Data.SqlClient的ProviderManifestToken =2008的xmlns:商店=htt​​p://schemas.microsoft.com/ado/2007/12/ EDM / EntityStoreSchemaGenerator的xmlns =http://schemas.microsoft.com/ado/2009/02/edm/ssdl>
        < EntityContainer相关名称=Ef2PlaygroundModel_3StoreContainer>
          < EntitySet的名称=汽车总动员的EntityType =Ef2PlaygroundModel_3.Store.Cars店:TYPE =表模式=DBO/>
          < EntitySet的名称=驱动程序的EntityType =Ef2PlaygroundModel_3.Store.Drivers店:TYPE =表模式=DBO/>
          < EntitySet的名称=CarDriver的EntityType =Ef2PlaygroundModel_3.Store.CarDriver店:TYPE =表模式=DBO/>
          < AssociationSet名称=FK_CarDriver_Car协会=Ef2PlaygroundModel_3.Store.FK_CarDriver_Car>
            <作用到底=车的EntitySet =汽车/>
            <作用到底=CarDriver的EntitySet =CarDriver/>
          < / AssociationSet>
          < AssociationSet名称=FK_CarDriver_Driver协会=Ef2PlaygroundModel_3.Store.FK_CarDriver_Driver>
            <作用到底=驱动程序的EntitySet =驱动程序/>
            <作用到底=CarDriver的EntitySet =CarDriver/>
          < / AssociationSet>
        < /&EntityContainer相关GT;
        <的EntityType名称=汽车总动员>
          <钥匙>
            < PropertyRef名称=ID/>
          < /钥匙>
          <属性名=ID类型=INTStoreGeneratedPattern =身份可空=FALSE/>
          <属性名=品牌TYPE =为nvarchar(max)可空=FALSE/>
          <属性名=型号TYPE =为nvarchar(max)可空=FALSE/>
          <属性名=RELEASEDATETYPE =日期时间可空=真/>
        < /的EntityType>
        <的EntityType名称=驱动程序>
          <钥匙>
            < PropertyRef名称=ID/>
          < /钥匙>
          <属性名=ID类型=INTStoreGeneratedPattern =身份可空=FALSE/>
          <属性名=姓名类型=为nvarchar(max)可空=FALSE/>
        < /的EntityType>
        <的EntityType名称=CarDriver>
          <钥匙>
            < PropertyRef名称=Cars_Id/>
            < PropertyRef名称=Drivers_Id/>
          < /钥匙>
          <属性名=Cars_IdTYPE =INT可空=FALSE/>
          <属性名=Drivers_IdTYPE =INT可空=FALSE/>
        < /的EntityType>
        <协会名称=FK_CarDriver_Car>
          <作用到底=车类型=Ef2PlaygroundModel_3.Store.Cars多重=1/>
          <作用到底=CarDriverTYPE =Ef2PlaygroundModel_3.Store.CarDriver多重=*/>
          < ReferentialConstraint>
            <主体作用=汽车与GT;
              < PropertyRef名称=ID/>
            < /首席>
            <依赖的角色=CarDriver>
              < PropertyRef名称=Cars_Id/>
            < /从属>
          < / ReferentialConstraint>
        < /协会>
        <协会名称=FK_CarDriver_Driver>
          <作用到底=CarDriverTYPE =Ef2PlaygroundModel_3.Store.CarDriver多重=*/>
          <作用到底=驱动程序类型=Ef2PlaygroundModel_3.Store.Drivers多重=1/>
          < ReferentialConstraint>
            <主体作用=驱动程序>
              < PropertyRef名称=ID/>
            < /首席>
            <依赖的角色=CarDriver>
              < PropertyRef名称=Drivers_Id/>
            < /从属>
          < / ReferentialConstraint>
        < /协会>
      < /架构>
    < / EDMX:StorageModels>
    <! -  CSDL内容 - >
    < EDMX:ConceptualModels>
      <模式的xmlns =htt​​p://schemas.microsoft.com/ado/2008/09/edm的xmlns:CG =htt​​p://schemas.microsoft.com/ado/2006/04/$c$cgeneration的xmlns:店内=htt​​p://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator命名空间=Ef2PlaygroundModel_3别名=自我的xmlns:诠释=htt​​p://schemas.microsoft.com/ ADO / 2009/02 / EDM /注释>
        < EntityContainer相关名称=Ef2PlaygroundModel_3Container注释:LazyLoadingEnabled =真正的>
          < EntitySet的名称=汽车总动员的EntityType =Ef2PlaygroundModel_3.Car/>
          < EntitySet的名称=驱动程序的EntityType =Ef2PlaygroundModel_3.Driver/>
          < AssociationSet名称=CarDriver协会=Ef2PlaygroundModel_3.CarDriver>
            <作用到底=车的EntitySet =汽车/>
            <作用到底=驱动程序的EntitySet =驱动程序/>
          < / AssociationSet>
        < /&EntityContainer相关GT;
        <的EntityType名称=汽车与GT;
          <钥匙>
            < PropertyRef名称=ID/>
          < /钥匙>
          <物业类型=的Int32NAME =ID可空=假的注释:StoreGeneratedPattern =身份/>
          <物业类型=字符串NAME =牌可空=FALSE/>
          <物业类型=字符串NAME =模型可空=FALSE/>
          <物业类型=日期时间NAME =RELEASEDATE可空=真/>
          < NavigationProperty名称=驱动程序的关系=Ef2PlaygroundModel_3.CarDriverFromRole =汽车ToRole =驱动程序/>
        < /的EntityType>
        <的EntityType名称=驱动程序>
          <钥匙>
            < PropertyRef名称=ID/>
          < /钥匙>
          <物业类型=的Int32NAME =ID可空=假的注释:StoreGeneratedPattern =身份/>
          <物业类型=字符串名称=名称可空=FALSE/>
          < NavigationProperty名称=汽车总动员的关系=Ef2PlaygroundModel_3.CarDriverFromRole =驱动程序ToRole =汽车/>
        < /的EntityType>
        <协会名称=CarDriver>
          <结束类型=Ef2PlaygroundModel_3.Car角色=汽车多重=*/>
          <结束类型=Ef2PlaygroundModel_3.Driver角色=驱动程序多重=*/>
        < /协会>
      < /架构>
    < / EDMX:ConceptualModels>
    <  - !C-S映射的内容 - >
    < EDMX:映射>
      <映射空间=CS的xmlns =htt​​p://schemas.microsoft.com/ado/2008/09/mapping/cs>
        < EntityContainerMapping StorageEntityContainer =Ef2PlaygroundModel_3StoreContainerCdmEntityContainer =Ef2PlaygroundModel_3Container>
          < EntitySetMapping名称=汽车总动员>
            < EntityTypeMapping类型名=IsTypeOf(Ef2PlaygroundModel_3.Car)>
              < MappingFragment StoreEntitySet =汽车总动员>
                < ScalarProperty名称=ID的ColumnName =ID/>
                < ScalarProperty名称=品牌的ColumnName =品牌/>
                < ScalarProperty名称=模型的ColumnName =模式/>
                < ScalarProperty名称=RELEASEDATE的ColumnName =RELEASEDATE/>
              < / MappingFragment>
            < / EntityTypeMapping>
          < / EntitySetMapping>
          < EntitySetMapping名称=驱动程序>
            < EntityTypeMapping类型名=IsTypeOf(Ef2PlaygroundModel_3.Driver)>
              < MappingFragment StoreEntitySet =驱动程序>
                < ScalarProperty名称=ID的ColumnName =ID/>
                < ScalarProperty名称=名称的ColumnName =名称/>
              < / MappingFragment>
            < / EntityTypeMapping>
          < / EntitySetMapping>
          < AssociationSetMapping名称=CarDriver类型名=Ef2PlaygroundModel_3.CarDriverStoreEntitySet =CarDriver>
            < EndProperty名称=汽车与GT;
              < ScalarProperty名称=ID的ColumnName =Cars_Id/>
            < / EndProperty>
            < EndProperty名称=驱动程序>
              < ScalarProperty名称=ID的ColumnName =Drivers_Id/>
            < / EndProperty>
          < / AssociationSetMapping>
        < / EntityContainerMapping>
      < /映射>
    < / EDMX:映射>
  < / EDMX:运行>
  <! -  EF设计内容(不要手动编辑低于HERE) - >
  < EDMX:设计师的xmlns =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
    < EDMX:连接>
      < D​​esignerInfoPropertySet>
        < D​​esignerProperty名称=MetadataArtifactProcessingVALUE =EmbedInOutputAssembly/>
      < / DesignerInfoPropertySet>
    < / EDMX:连接>
    < EDMX:选项>
      < D​​esignerInfoPropertySet>
        < D​​esignerProperty名称=ValidateOnBuildVALUE =真/>
        < D​​esignerProperty名称=EnablePluralizationVALUE =FALSE/>
        < D​​esignerProperty NAME =codeGenerationStrategyVALUE =无/>
      < / DesignerInfoPropertySet>
    < / EDMX:选项>
    <! - 图的内容(形状和连接器的位置) - >
    < EDMX:图>
      <图名称=Ef2PlaygroundModel_3>
        < EntityTypeShape的EntityType =Ef2PlaygroundModel_3.CarWIDTH =1.5PointX =3.25尖尖=1.625高度=1.787985026041667/>
        < EntityTypeShape的EntityType =Ef2PlaygroundModel_3.DriverWIDTH =1.5PointX =5.375尖尖=1.625高度=1.59568359375/>
        < AssociationConnector协会=Ef2PlaygroundModel_3.CarDriver>
          < ConnectorPoint PointX =4.75尖尖=2.422841796875/>
          < ConnectorPoint PointX =5.375尖尖=2.422841796875/>
        < / AssociationConnector>
      < /图>
    < / EDMX:图>
  < / EDMX:设计师>
< / EDMX:EDMX>
 

的app.config:

 <结构>
  <的ConnectionStrings>
    <添加
      NAME =Ef2PlaygroundModel_3Container
      connectionString="metadata=res://*/Ef2PlaygroundModel_3.csdl|res://*/Ef2PlaygroundModel_3.ssdl|res://*/Ef2PlaygroundModel_3.msl;provider=System.Data.SqlClient;provider连接字符串=安培; QUOT;数据源= \ SqlEx preSS;初始目录= Ef2PlaygroundModel_3;集成安全性= TRUE; MultipleActiveResultSets =真放; QUOT;
      的providerName =System.Data.EntityClient
    />
  < /的ConnectionStrings>
< /结构>
 

Ef2PlaygroundModel_3Pocos.cs:

 使用系统;
使用System.Collections.Generic;
使用System.Data.Objects;

命名空间EF2_Playground
{
  公共类车
  {
    公共车()
    {
      驱动程序=新的名单,其中,驱动程序和GT;();
    }

    公众诠释编号{获得;组; }
    公共字符串品牌{获得;组; }
    公共字符串型号{获得;组; }
    公开日期时间? RELEASEDATE {获得;组; }

    公共虚拟目录<驱动>司机{获得;私定; }
  }

  公共类驱动程序
  {
    公共驱动器()
    {
      汽车=新的名单,其中,汽车及GT;();
    }

    公众诠释编号{获得;组; }
    公共字符串名称{;组; }

    公共虚拟目录<车>汽车{获得;私定; }
  }

  公共类Ef2PlaygroundModel_3Container:ObjectContext的
  {
    公共Ef2PlaygroundModel_3Container()
      :基地(NAME = Ef2PlaygroundModel_3Container)
    {
      ContextOptions.LazyLoadingEnabled = TRUE;
    }

    公共IObjectSet<车>汽车
    {
      {返回CreateObjectSet<车>(); }
    }

    公共IObjectSet<驱动>司机
    {
      {返回CreateObjectSet<驱动>(); }
    }
  }
}
 

Program.cs的:

 使用系统;

命名空间EF2_Playground
{
  类节目
  {
    静态无效的主要(字串[] args)
    {
      使用(VAR CTX =新Ef2PlaygroundModel_3Container())
      {
        的foreach(在ctx.Drivers VAR驱动程序)
        {
          Console.WriteLine(driver.Name);
          的foreach(VAR车driver.Cars)
          {
            Console.WriteLine(驱动{0}  -  {1}(发布{2}),car.Brand,car.Model,car.ReleaseDate);
          }
        }
      }
    }
  }
}
 

最后Model1.edmx,一旦我把它添加到项目中打破了整个事情:

 < XML版本=1.0编码=UTF-8&GT?;
< EDMX:EDMX版本=2.0的xmlns:EDMX =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
  <! -  EF运行时的内容 - >
  < EDMX:运行>
    <! -  SSDL内容 - >
    < EDMX:StorageModels>
      <模式的xmlns =htt​​p://schemas.microsoft.com/ado/2009/02/edm/ssdl命名空间=Model1.Store别名=自我提供程序=System.Data.SqlClient的ProviderManifestToken = 2005年>
        < EntityContainer相关名称=Model1TargetContainer>
        < /&EntityContainer相关GT;
      < /架构>
    < / EDMX:StorageModels>
    <! -  CSDL内容 - >
    < EDMX:ConceptualModels>
      <模式的xmlns =htt​​p://schemas.microsoft.com/ado/2008/09/edm的xmlns:CG =htt​​p://schemas.microsoft.com/ado/2006/04/$c$cgeneration的xmlns:店内=htt​​p://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator命名空间=型号1别名=自我的xmlns:诠释=htt​​p://schemas.microsoft.com/ ADO / 2009/02 / EDM /注释>
        < EntityContainer相关名称=Model1Container注释:LazyLoadingEnabled =真正的>
        < /&EntityContainer相关GT;
      < /架构>
    < / EDMX:ConceptualModels>
    <  - !C-S映射的内容 - >
    < EDMX:映射>
      <映射的xmlns =htt​​p://schemas.microsoft.com/ado/2008/09/mapping/cs空间=CS>
        <别名重点=模型VALUE =型号1/>
        <别名重点=目标值=Model1.Store/>
        < EntityContainerMapping CdmEntityContainer =Model1ContainerStorageEntityContainer =Model1TargetContainer>
        < / EntityContainerMapping>
      < /映射>
    < / EDMX:映射>
  < / EDMX:运行>
  <! -  EF设计内容(不要手动编辑低于HERE) - >
  < EDMX:设计师的xmlns =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
    < EDMX:连接>
      < D​​esignerInfoPropertySet>
        < D​​esignerProperty名称=MetadataArtifactProcessingVALUE =EmbedInOutputAssembly/>
      < / DesignerInfoPropertySet>
    < / EDMX:连接>
    < EDMX:选项>
      < D​​esignerInfoPropertySet>
        < D​​esignerProperty名称=ValidateOnBuildVALUE =真/>
        < D​​esignerProperty名称=EnablePluralizationVALUE =FALSE/>
      < / DesignerInfoPropertySet>
    < / EDMX:选项>
    <! - 图的内容(形状和连接器的位置) - >
    < EDMX:图>
      <图名称=型号1/>
    < / EDMX:图>
  < / EDMX:设计师>
< / EDMX:EDMX>
 

解决方案

好吧,我想我已经得到它。我减少了第二个模型(一个制动器项目)于以下内容:

的Class1.cs

 使用System.Data.Objects.DataClasses;
[总成:EdmSchemaAttribute()]
 

砰!出现众所周知的例外。

由于在许多情况下,阅读文档帮助:

  不支持

映射POCO实体,如果任何映射属性应用到自定义数据类,包括EdmSchemaAttribute在装配水平。

当然,我不要随便添加映射属性自定义数据类,但这并不重要的EdmSchemaAttribute因为,一个住在装配水平。

添加第二个非POCO模式导致code一代导致包含(至少)EdmSchemaAttribute和不支持的类。

我学到的东西:不要混合使用POCO和非POCO模型在一个组装

I have a pretty simple console project with an entity model (containing two simple entities), two handmade POCOs and a handmade Context class. The program fires a simple query against the DB and everything including LazyLoading works fine.

The problem: As soon as i add another Entity data model (even if i add an empty one), the calls to CreateObjectSet in Ef2PlaygroundModel_3Container throw the following exception:

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

Does anyone have an idea about what is going wrong here?


That is the working project:

Ef2PlaygroundModel_3.edmx:

Code Generation Strategy is set to "None"

<?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>(); }
    }
  }
}

Program.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);
          }
        }
      }
    }
  }
}

And finally Model1.edmx that breaks the whole thing as soon as i add it to the project:

<?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>

解决方案

Ok, i guess i've got it. I reduced the second model (the one that brakes the project) to the following:

Class1.cs

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

Bang! The well known exception appears.

As in so many cases, reading the documentation helps:

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

Sure, i do not literally add mapping attributes to CUSTOM data classes but that doesn't matter for the EdmSchemaAttribute since that one lives on the assembly level.

Adding the second non-POCO model causes code generation resulting in a class that contains (at least) the EdmSchemaAttribute and that is not supported.

What i've learned: Don't mix POCO and non-POCO models in one assembly.

这篇关于EF4 POCO(不使用T4):映射和元数​​据信息无法找到的EntityType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆