【问题标题】:Entity - Schema Mapping and Namespaces实体 - 模式映射和命名空间
【发布时间】:2012-10-19 16:04:21
【问题描述】:

向我的 DAL 项目添加了一个数据库和一个实体模型。

出现错误:

错误:

 +      base    {"Schema specified is not valid. Errors: \r\nMultiple types with the name 'TblRecord' exist in the EdmItemCollection in different namespaces.  Convention based mapping requires unique names without regard to namespace in the EdmItemCollection."}   System.Data.EntityException {System.Data.MetadataException}

Edmx 文件

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="Xz.Business.Matches.Store" Alias="Self" Provider="System.Data.SqlServerCe.4.0" ProviderManifestToken="4.0" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
        <EntityContainer Name="XzBusinessMatchesStoreContainer">
          <EntitySet Name="Records" EntityType="Xz.Business.Matches.Store.Records" store:Type="Tables" />
        </EntityContainer>
        <EntityType Name="Records">
          <Key>
            <PropertyRef Name="Record" />
          </Key>
          <Property Name="Record" Type="nvarchar" Nullable="false" MaxLength="100" />
          <Property Name="Relations" Type="nvarchar" MaxLength="450" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="Xz.Business.Matches" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <EntityContainer Name="RecordzEntities" p1:LazyLoadingEnabled="true">
          <EntitySet Name="Records" EntityType="Xz.Business.Matches.TblRecord" />
        </EntityContainer>
        <EntityType Name="TblRecord">
          <Key>
            <PropertyRef Name="Record" />
          </Key>
          <Property Name="Record" Type="String" Nullable="false" MaxLength="100" Unicode="true" FixedLength="false" />
          <Property Name="Relations" Type="String" MaxLength="450" Unicode="true" FixedLength="false" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="XzBusinessMatchesStoreContainer" CdmEntityContainer="RecordzEntities">
          <EntitySetMapping Name="Records">
            <EntityTypeMapping TypeName="Xz.Business.Matches.TblRecord">
              <MappingFragment StoreEntitySet="Records">
                <ScalarProperty Name="Record" ColumnName="Record" />
                <ScalarProperty Name="Relations" ColumnName="Relations" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="True" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>

名称和命名空间:

项目名称:Business.Matches 默认命名空间:Xz.Business.Matches

我添加了一个具有此定义的实体模型:

模型:RecordzModel

上下文:RecordzContext

模型命名空间:Xz.Business.Matches

【问题讨论】:

    标签: c# entity-framework entity-framework-5


    【解决方案1】:

    您的项目中似乎有不止一个名为“TblRecord”的类。在将您的类型与 POCO 类型的 EDM 类型匹配时,命名空间将被忽略,因此如果您有多个具有相同名称的类,则它会模棱两可并导致异常。

    【讨论】:

    • 这就是错误所说的,我检查的第一件事,但没有 2 个 TblRecord,edmx 或其相关部分中没有任何可能与之冲突的内容吗?跨度>
    • 您是否有其他项目或对程序集的引用也可以包含 TblRecord 类?你能展示一个复制品吗?
    • 你到底是什么意思?如果我理解正确,有一个引用 DAL 的域类,它没有另一个 TblRecord 类。只需使用 DAL。
    • 听起来有点傻,但我看到了完全相同的示例,问题是使用了 2 个 .config 文件,其中一个在 DAL 中,一个在主应用程序中。
    猜你喜欢
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2012-05-25
    • 1970-01-01
    • 2013-12-19
    相关资源
    最近更新 更多