代码之家  ›  专栏  ›  技术社区  ›  Chris Klepeis

实体框架-实体映射问题

  •  3
  • Chris Klepeis  · 技术社区  · 16 年前

    I have two tables: Address and Contact which are joined on contactID (in Contact). Both of these tables have entities in my Entity data model (EF 4.0) and I do not wan't to modify them.

    我确实想创建一个新实体,其中包含来自两个实体的信息。

    到目前为止我所做的:

    在CSDL:

    <EntityContainer...>
        <EntitySet Name="AddressTest" EntityType="WebGearsModel.Test" />
        <EntitySet Name="ContactTest" EntityType="WebGearsModel.Test" />
    </EntityContainer>
    
    <EntityType Name="Test">
      <Key>
        <PropertyRef Name="addressID" />
      </Key>
      <Property Type="Int32" Name="addressID" Nullable="false" annotation:StoreGeneratedPattern="Identity"  />
      <Property Type="Int32" Name="contactID" Nullable="false"  />
      <Property Type="String" Name="firstName" Nullable="false" MaxLength="30" FixedLength="false" Unicode="false" />
      <Property Type="String" Name="emailAddress" Nullable="false" MaxLength="150" FixedLength="false" Unicode="false" />
    </EntityType>
    

    在我的C-S映射中:

    <EntitySetMapping Name="AddressTest">
      <EntityTypeMapping TypeName="WebGearsModel.Test">
        <MappingFragment StoreEntitySet="Address">
          <ScalarProperty Name="addressID" ColumnName="addressID" />
          <ScalarProperty Name="contactID" ColumnName="contactID" />
          <ScalarProperty Name="firstName" ColumnName="firstName" />
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    
    <EntitySetMapping Name="ContactTest">
      <EntityTypeMapping TypeName="WebGearsModel.Test">
        <MappingFragment StoreEntitySet="Contact">
          <ScalarProperty Name="contactID" ColumnName="contactID" />
          <ScalarProperty Name="emailAddress" ColumnName="emailAddress" />
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    

    我收到的错误是:

    开始映射片段时出现问题 在第150行:必须为指定映射 所有键属性 (contacttest.addressID)的 EntitySet联系人测试。

    当联系人实体中不存在地址ID时,如何从该实体映射该地址ID?我想我需要某种联系,但我不确定该怎么做…记住,我不想修改我现有的地址和联系人实体。

    1 回复  |  直到 16 年前
        1
  •  4
  •   jrista    16 年前

    记住实体的定义:

    不由其定义的对象 属性,而不是 连续性及其特性。

    每个“实体”都必须有唯一标识它的东西;一个键。但是,您似乎试图从一个物理类型中定义两种类型的实体,该物理类型只有一个键,为地址提供一致的标识,而不是联系人。这违反了实体的规则,使contacttest概念无效。

    由于基础物理类型, Test ,定义键属性, addressID , all EntitySet's derived from that type must map that property to conform to the rules defining an Entity. Maintaining consistency of state is impossible otherwise.

    推荐文章