如何从实体框架实体获取Discriminator Column的值

时间:2009-06-18 21:39:42

标签: entity-framework

我在EDM中写了一个项目。有谁知道如何从TPH继承树中的实体获取鉴别器列的值?我无法将该属性添加到实体。有没有其他方法可以获得价值?

谢谢,
罗伊

1 个答案:

答案 0 :(得分:2)

有一种简单的方法可以解决支持可更新视图的DBMS问题。 您只需创建一个视图或具有附加鉴别器列的定义查询。 原始列应映射到类属性。如果DBMS不支持可更新视图,您可以使用“定义查询”,然后映射存储过程以进行插入/更新/删除操作。

以下是Oracle数据库的示例:

SQL:
CREATE TABLE TEST.TPH_TABLE (
  ID NUMBER(9),
  COLUMN_A VARCHAR2(20),
  COLUMN_B VARCHAR2(20),
  BASE_COLUMN VARCHAR2(20),
  CONSTRAINT PK_TPH_TABLE PRIMARY KEY (ID)
);

EDMX:
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="Model1.Store" Alias="Self" Provider="Devart.Data.Oracle" ProviderManifestToken="ORA" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
        <EntityContainer Name="Model1StoreContainer">
          <EntitySet Name="TPH_TABLE" EntityType="Model1.Store.TPH_TABLE" >
            <DefiningQuery>
              SELECT ID, BASE_COLUMN, COLUMN_A, COLUMN_B,
                     CASE WHEN COLUMN_A IS NOT NULL THEN '1' ELSE NULL END AS "IS_TABLE_A"
                FROM TPH_TABLE
            </DefiningQuery>
          </EntitySet>
        </EntityContainer>
        <EntityType Name="TPH_TABLE">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="int" Nullable="false" />
          <Property Name="BASE_COLUMN" Type="VARCHAR2" MaxLength="20" />
          <Property Name="COLUMN_A" Type="VARCHAR2" MaxLength="20" />
          <Property Name="COLUMN_B" Type="VARCHAR2" MaxLength="20" />
          <Property Name="IS_TABLE_A" Type="VARCHAR2" MaxLength="1" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="Model1" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
        <EntityContainer Name="Entities">
          <EntitySet Name="TPH_TABLE" EntityType="Model1.TPH_TABLE" />
        </EntityContainer>
        <EntityType Name="TPH_TABLE" Abstract="true">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="Int32" Nullable="false" />
          <Property Name="BASE_COLUMN" Type="String" MaxLength="20" Unicode="true" FixedLength="false" />
        </EntityType>
        <EntityType Name="TABLE_A" BaseType="Model1.TPH_TABLE" >
          <Property Name="COLUMN_A" Type="String" Nullable="true" />
        </EntityType>
        <EntityType Name="TABLE_B" BaseType="Model1.TPH_TABLE" >
          <Property Name="COLUMN_B" Type="String" Nullable="true" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
        <EntityContainerMapping StorageEntityContainer="Model1StoreContainer" CdmEntityContainer="Entities">
          <EntitySetMapping Name="TPH_TABLE">
            <EntityTypeMapping TypeName="Model1.TABLE_A">
              <MappingFragment StoreEntitySet="TPH_TABLE">
                <ScalarProperty Name="ID" ColumnName="ID" />
                <ScalarProperty Name="BASE_COLUMN" ColumnName="BASE_COLUMN" />
                <ScalarProperty Name="COLUMN_A" ColumnName="COLUMN_A" />
                <Condition ColumnName="IS_TABLE_A" Value="1" />
              </MappingFragment>
            </EntityTypeMapping>
            <EntityTypeMapping TypeName="Model1.TABLE_B">
              <MappingFragment StoreEntitySet="TPH_TABLE">
                <ScalarProperty Name="ID" ColumnName="ID" />
                <ScalarProperty Name="BASE_COLUMN" ColumnName="BASE_COLUMN" />
                <ScalarProperty Name="COLUMN_B" ColumnName="COLUMN_B" />
                <Condition ColumnName="IS_TABLE_A" IsNull="true" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="Model4">
        <EntityTypeShape EntityType="Model1.TPH_TABLE" Width="1.5" PointX="3" PointY="0.5" Height="1.2636116536458335" IsExpanded="true" />
        <EntityTypeShape EntityType="Model1.TABLE_A" Width="1.5" PointX="1.75" PointY="2.75" Height="1.099264322916667" />
        <EntityTypeShape EntityType="Model1.TABLE_B" Width="1.5" PointX="4.25" PointY="2.75" Height="1.099264322916667" />
        <InheritanceConnector EntityType="Model1.TABLE_B" ManuallyRouted="false">
          <ConnectorPoint PointX="4.375" PointY="1.7636116536458335" />
          <ConnectorPoint PointX="4.375" PointY="2.75" />
          </InheritanceConnector>
        <InheritanceConnector EntityType="Model1.TABLE_A" ManuallyRouted="false">
          <ConnectorPoint PointX="3.125" PointY="1.7636116536458335" />
          <ConnectorPoint PointX="3.125" PointY="2.75" />
          </InheritanceConnector></Diagram></edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>