views:

464

answers:

1

Hi,
I've got a project in written in the EDM. Does anyone know how to get the value of the discriminator column from an entity in a TPH inheritance tree? I can't add the property to the entity. Is there any other way to get the value?

Thanks,
Roy

+2  A: 

There is a simple way to solve the problem for the DBMS that suports updatable views. You can simply create a view or a Defining Query having an additional discriminator column. The original column should be mapped to the class property. In case when DBMS does not support the updatable views you can use Defining Query and then map stored procedures for Insert/Update/Delete operations.

Here is an example for Oracle database:

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"&gt;
  <!-- 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"&gt;
        <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"&gt;
        <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"&gt;
    <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>
Devart