具有多重抽象继承的实体框架 TPH

发布于 2024-08-14 07:26:52 字数 368 浏览 7 评论 0原文

我正在尝试在实体框架(VS 2008 sp1,3.5)中创建一个表每个层次结构模型。

我的大多数模型都非常简单,是一个抽象类型,具有从中继承的多个子类型。

然而,我一直在努力应对这个最新的挑战。我有一些学生,我想从 PERSONS(抽象)继承,而这些学生应该从 PARTIES(抽象)继承。

每次执行此操作时,我都会收到“错误 2078:EntityType 'Model.PERSONS' 是抽象的,只能使用 IsTypeOf 进行映射”。我猜问题是 PARTIES 已经在实体集中定义为 IsTypeOf 。

那么这可能吗?我可以通过使 PERSONS Abstract = false 并分配虚假的条件映射来解决这个问题。但这似乎是一个愚蠢的解决方法。

I'm trying to do a Table Per Hierarchy model in Entity Framework(VS 2008 sp1, 3.5).

Most of my models have been very simple, an abstract type with multiple sub-types that inherit from it.

However, I've been struggling with this latest challenge. I have STUDENTS that I'd like to inherit from PERSONS(abstract) that should inherity from PARTIES(abstract).

Every time I do this I get a "Error 2078: The EntityType 'Model.PERSONS' is Abstract and can be mapped only using IsTypeOf." I guess the problem is PARTIES is already defined as IsTypeOf in the entity set.

So is this even possible? I can work around it by making PERSONS abstract = false and assigning a bogus conditional mapping. But this seems like a silly workaround.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-08-21 07:26:52

使用 XML 编辑器编辑模型:找到映射并将 IsTypeOf 添加到相应的 EntityTypeMapping 标记。
这是与您的情况类似的示例:
SQL Server DDL:

CREATE TABLE [dbo].[Student] (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [PartyInfo] [varchar](max) NOT NULL,
    [PersonInfo] [varchar](max) NOT NULL,
    [StudInfo] [varchar](max) NOT NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED (  [Id] ASC  )
  )

EDMX:

<?xml version="1.0" encoding="utf-8"?>
<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="test_1Model.Store" Alias="Self" 
Provider="System.Data.SqlClient" ProviderManifestToken="2005" 
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" 
xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    <EntityContainer Name="test_1ModelStoreContainer">
      <EntitySet Name="Student" EntityType="test_1Model.Store.Student" 
store:Type="Tables" Schema="dbo" />
    </EntityContainer>
    <EntityType Name="Student">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="int" Nullable="false" 
StoreGeneratedPattern="Identity" />
      <Property Name="PartyInfo" Type="varchar(max)" Nullable="false" />
      <Property Name="PersonInfo" Type="varchar(max)" Nullable="false" />
      <Property Name="StudInfo" Type="varchar(max)" Nullable="false" />
    </EntityType>
  </Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
  <Schema Namespace="test_1Model" Alias="Self" 
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
    <EntityContainer Name="test_Entities">
      <EntitySet Name="PartySet" EntityType="test_1Model.Party" />
    </EntityContainer>
    <EntityType Name="Party" Abstract="true">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="Int32" Nullable="false" />
      <Property Name="PartyInfo" Type="String" Nullable="false" 
MaxLength="Max" Unicode="false" FixedLength="false" />
    </EntityType>
    <EntityType Name="Person" BaseType="test_1Model.Party" Abstract="true" >
      <Property Name="PersonInfo" Type="String" Nullable="false" />
    </EntityType>
    <EntityType Name="Student" BaseType="test_1Model.Person" >
      <Property Name="StudInfo" Type="String" Nullable="false" />
    </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="test_1ModelStoreContainer" 
CdmEntityContainer="test_Entities">
      <EntitySetMapping Name="PartySet">
        <EntityTypeMapping TypeName="IsTypeOf(test_1Model.Party)">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
            <ScalarProperty Name="Id" ColumnName="Id" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(test_1Model.Person)">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="Id" ColumnName="Id" />
            <ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="test_1Model.Student">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
            <ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
            <ScalarProperty Name="Id" ColumnName="Id" />
            <ScalarProperty Name="StudInfo" ColumnName="StudInfo" />
          </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="SqlServer_Model" />
  </edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>

Edit the model using XML Editor: find the mapping and add IsTypeOf to the corresponding EntityTypeMapping tag.
Here is an example resembling your case:
SQL Server DDL:

CREATE TABLE [dbo].[Student] (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [PartyInfo] [varchar](max) NOT NULL,
    [PersonInfo] [varchar](max) NOT NULL,
    [StudInfo] [varchar](max) NOT NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED (  [Id] ASC  )
  )

EDMX:

<?xml version="1.0" encoding="utf-8"?>
<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="test_1Model.Store" Alias="Self" 
Provider="System.Data.SqlClient" ProviderManifestToken="2005" 
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" 
xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    <EntityContainer Name="test_1ModelStoreContainer">
      <EntitySet Name="Student" EntityType="test_1Model.Store.Student" 
store:Type="Tables" Schema="dbo" />
    </EntityContainer>
    <EntityType Name="Student">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="int" Nullable="false" 
StoreGeneratedPattern="Identity" />
      <Property Name="PartyInfo" Type="varchar(max)" Nullable="false" />
      <Property Name="PersonInfo" Type="varchar(max)" Nullable="false" />
      <Property Name="StudInfo" Type="varchar(max)" Nullable="false" />
    </EntityType>
  </Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
  <Schema Namespace="test_1Model" Alias="Self" 
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
    <EntityContainer Name="test_Entities">
      <EntitySet Name="PartySet" EntityType="test_1Model.Party" />
    </EntityContainer>
    <EntityType Name="Party" Abstract="true">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="Int32" Nullable="false" />
      <Property Name="PartyInfo" Type="String" Nullable="false" 
MaxLength="Max" Unicode="false" FixedLength="false" />
    </EntityType>
    <EntityType Name="Person" BaseType="test_1Model.Party" Abstract="true" >
      <Property Name="PersonInfo" Type="String" Nullable="false" />
    </EntityType>
    <EntityType Name="Student" BaseType="test_1Model.Person" >
      <Property Name="StudInfo" Type="String" Nullable="false" />
    </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="test_1ModelStoreContainer" 
CdmEntityContainer="test_Entities">
      <EntitySetMapping Name="PartySet">
        <EntityTypeMapping TypeName="IsTypeOf(test_1Model.Party)">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
            <ScalarProperty Name="Id" ColumnName="Id" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(test_1Model.Person)">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="Id" ColumnName="Id" />
            <ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="test_1Model.Student">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
            <ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
            <ScalarProperty Name="Id" ColumnName="Id" />
            <ScalarProperty Name="StudInfo" ColumnName="StudInfo" />
          </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="SqlServer_Model" />
  </edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文