2013-10-29 41 views
0

我正在使用.NET 4.5中的Entity Framework 5.0。我試圖建立一個表類型(TPT)繼承層次結構,其中我有一個外鍵到其中一個子類的主鍵。不幸的是,實體框架給我以下編譯錯誤:如何在TPT繼承層次結構中爲子類型定義外鍵

Error 3024: Problem in mapping fragments starting at line 163:Must specify mapping for all key properties (Id) of End Derived1 in Relationship FK_Items_Derived1.

我創建了一個測試數據庫和EF模型來顯示演示模型。我的數據庫模型是這樣的:

enter image description here

創建實體框架模型I:

  • 添加這些表到實體框架設計;
  • 選擇Base作爲Derived1Derived2上的基本類型;
  • 去除Derived2Base(因爲現在有一個繼承關係)之間Derived1Base和 之間的外鍵關係;
  • 我從Derived1Derived2中刪除Id屬性(因爲它們繼承BaseId屬性)。

這導致如下模式:

enter image description here

現在,當我編譯它,實體框架促使我與前面所述編譯錯誤:

Error 3024: Problem in mapping fragments starting at line 163:Must specify mapping for all key properties (Id) of End Derived1 in Relationship FK_Items_Derived1.

的錯誤似乎請指向Model的XML映射中的以下行:

<AssociationSetMapping Name="FK_Items_Derived1" 
    TypeName="TestModel.FK_Items_Derived1" StoreEntitySet="Items"> 
    <EndProperty Name="Items"> 
     <ScalarProperty Name="Id" ColumnName="Id" /> 
    </EndProperty> 
</AssociationSetMapping> 

我顯然不希望Items映射到Base類,因爲只有Derived1有項目,而不是Derived2。我不明白爲什麼實體框架設計者不能處理這個相當常見的用例。

所以當然問題是如何解決這個問題?

是完整的,下面是完整的實體框架映射文件:

<?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="TestModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"> 
     <EntityContainer Name="TestModelStoreContainer"> 
      <EntitySet Name="Base" EntityType="TestModel.Store.Base" store:Type="Tables" Schema="dbo" /> 
      <EntitySet Name="Derived1" EntityType="TestModel.Store.Derived1" store:Type="Tables" Schema="dbo" /> 
      <EntitySet Name="Derived2" EntityType="TestModel.Store.Derived2" store:Type="Tables" Schema="dbo" /> 
      <EntitySet Name="Items" EntityType="TestModel.Store.Items" store:Type="Tables" Schema="dbo" /> 
      <AssociationSet Name="FK_Derived1_Base" Association="TestModel.Store.FK_Derived1_Base"> 
      <End Role="Base" EntitySet="Base" /> 
      <End Role="Derived1" EntitySet="Derived1" /> 
      </AssociationSet> 
      <AssociationSet Name="FK_Derived2_Base" Association="TestModel.Store.FK_Derived2_Base"> 
      <End Role="Base" EntitySet="Base" /> 
      <End Role="Derived2" EntitySet="Derived2" /> 
      </AssociationSet> 
      <AssociationSet Name="FK_Items_Derived1" Association="TestModel.Store.FK_Items_Derived1"> 
      <End Role="Derived1" EntitySet="Derived1" /> 
      <End Role="Items" EntitySet="Items" /> 
      </AssociationSet> 
     </EntityContainer> 
     <EntityType Name="Base"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" /> 
     </EntityType> 
     <EntityType Name="Derived1"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Length" Type="int" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Derived2"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Size" Type="int" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Items"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Derived1Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Description" Type="nvarchar" Nullable="false" MaxLength="50" /> 
     </EntityType> 
     <Association Name="FK_Derived1_Base"> 
      <End Role="Base" Type="TestModel.Store.Base" Multiplicity="1" /> 
      <End Role="Derived1" Type="TestModel.Store.Derived1" Multiplicity="0..1" /> 
      <ReferentialConstraint> 
      <Principal Role="Base"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Derived1"> 
       <PropertyRef Name="Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <Association Name="FK_Derived2_Base"> 
      <End Role="Base" Type="TestModel.Store.Base" Multiplicity="1" /> 
      <End Role="Derived2" Type="TestModel.Store.Derived2" Multiplicity="0..1" /> 
      <ReferentialConstraint> 
      <Principal Role="Base"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Derived2"> 
       <PropertyRef Name="Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <Association Name="FK_Items_Derived1"> 
      <End Role="Derived1" Type="TestModel.Store.Derived1" Multiplicity="1" /> 
      <End Role="Items" Type="TestModel.Store.Items" Multiplicity="*" /> 
      <ReferentialConstraint> 
      <Principal Role="Derived1"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Items"> 
       <PropertyRef Name="Derived1Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     </Schema> 
    </edmx:StorageModels> 
    <!-- CSDL content --> 
    <edmx:ConceptualModels> 
     <Schema Namespace="TestModel" 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="Entities" p1:LazyLoadingEnabled="true"> 
      <EntitySet Name="Base" EntityType="TestModel.Base" /> 
      <EntitySet Name="Items" EntityType="TestModel.Items" /> 
      <AssociationSet Name="FK_Items_Derived1" Association="TestModel.FK_Items_Derived1"> 
      <End Role="Derived1" EntitySet="Base" /> 
      <End Role="Items" EntitySet="Items" /> 
      </AssociationSet> 
     </EntityContainer> 
     <EntityType Name="Base"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="Guid" Nullable="false" /> 
      <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> 
     </EntityType> 
     <EntityType Name="Derived1" BaseType="TestModel.Base"> 
      <Property Name="Length" Type="Int32" Nullable="false" /> 
      <NavigationProperty Name="Items" Relationship="TestModel.FK_Items_Derived1" FromRole="Derived1" ToRole="Items" /> 
     </EntityType> 
     <EntityType Name="Derived2" BaseType="TestModel.Base"> 
      <Property Name="Size" Type="Int32" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Items"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="Guid" Nullable="false" /> 
      <Property Name="Description" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> 
      <NavigationProperty Name="Derived1" Relationship="TestModel.FK_Items_Derived1" FromRole="Items" ToRole="Derived1" /> 
     </EntityType> 
     <Association Name="FK_Items_Derived1"> 
      <End Role="Derived1" Type="TestModel.Derived1" Multiplicity="1" /> 
      <End Role="Items" Type="TestModel.Items" Multiplicity="*" /> 
     </Association> 
     </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="TestModelStoreContainer" CdmEntityContainer="Entities"> 
      <EntitySetMapping Name="Base"> 
      <EntityTypeMapping TypeName="IsTypeOf(TestModel.Base)"> 
       <MappingFragment StoreEntitySet="Base"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       <ScalarProperty Name="Name" ColumnName="Name" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      <EntityTypeMapping TypeName="IsTypeOf(TestModel.Derived2)"> 
       <MappingFragment StoreEntitySet="Derived2"> 
       <ScalarProperty Name="Size" ColumnName="Size" /> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      <EntityTypeMapping TypeName="IsTypeOf(TestModel.Derived1)"> 
       <MappingFragment StoreEntitySet="Derived1"> 
       <ScalarProperty Name="Length" ColumnName="Length" /> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      </EntitySetMapping> 
      <EntitySetMapping Name="Items"> 
      <EntityTypeMapping TypeName="TestModel.Items"> 
       <MappingFragment StoreEntitySet="Items"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       <ScalarProperty Name="Description" ColumnName="Description" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      </EntitySetMapping> 
      <AssociationSetMapping Name="FK_Items_Derived1" TypeName="TestModel.FK_Items_Derived1" StoreEntitySet="Items"> 
      <EndProperty Name="Items"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
      </EndProperty> 
      </AssociationSetMapping> 
     </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="False" /> 
     <DesignerProperty Name="IncludeForeignKeysInModel" Value="False" /> 
     <DesignerProperty Name="CodeGenerationStrategy" Value="None" /> 
     </DesignerInfoPropertySet> 
    </Options> 
    <!-- Diagram content (shape and connector positions) --> 
    <Diagrams></Diagrams> 
    </Designer> 
</edmx:Edmx> 

而且誰想要在本地重現此,這裏是DDL腳本:

CREATE TABLE [dbo].[Base](
    [Id] [uniqueidentifier] NOT NULL, 
    [Name] [nvarchar](50) NOT NULL, 
CONSTRAINT [PK_Base] PRIMARY KEY CLUSTERED ([Id] ASC) 
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 

CREATE TABLE [dbo].[Derived1](
    [Id] [uniqueidentifier] NOT NULL, 
    [Length] [int] NOT NULL, 
CONSTRAINT [PK_Derived1] PRIMARY KEY CLUSTERED ([Id] ASC) 
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 

CREATE TABLE [dbo].[Derived2](
    [Id] [uniqueidentifier] NOT NULL, 
    [Size] [int] NOT NULL, 
CONSTRAINT [PK_Derived2] PRIMARY KEY CLUSTERED ([Id] ASC) 
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 

CREATE TABLE [dbo].[Items](
    [Id] [uniqueidentifier] NOT NULL, 
    [Derived1Id] [uniqueidentifier] NOT NULL, 
    [Description] [nvarchar](50) NOT NULL, 
CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED ([Id] ASC) 
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 

ALTER TABLE [dbo].[Derived1] WITH CHECK ADD CONSTRAINT [FK_Derived1_Base] FOREIGN KEY([Id]) 
REFERENCES [dbo].[Base] ([Id]) 
GO 

ALTER TABLE [dbo].[Derived1] CHECK CONSTRAINT [FK_Derived1_Base] 
GO 

ALTER TABLE [dbo].[Derived2] WITH CHECK ADD CONSTRAINT [FK_Derived2_Base] FOREIGN KEY([Id]) 
REFERENCES [dbo].[Base] ([Id]) 
GO 

ALTER TABLE [dbo].[Derived2] CHECK CONSTRAINT [FK_Derived2_Base] 
GO 

ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Derived1] FOREIGN KEY([Derived1Id]) 
REFERENCES [dbo].[Derived1] ([Id]) 
GO 

ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Derived1] 
GO 
+0

這似乎是相關的:http://stackoverflow.com/questions/3783117/entityframework-mappings-what-is-wrong-with-this-mapping – Darek

回答

0

@ Jdev4ls指出我的答案。通過從模型中刪除主鍵(來自Derived1),實體框架缺少一些信息來進行協調。目前還不清楚我該如何避免這種情況,並且我相信設計人員應該做到這一點更簡單,因爲這是IMO非常常見的用例

但是,您可以更新XML或在設計師。通過上面的例子,去搜索以下XML:

<AssociationSetMapping Name="FK_Items_Derived1" 
    TypeName="tempdatabase1Model1.FK_Items_Derived1" StoreEntitySet="Items"> 
    <EndProperty Name="Items"> 
     <ScalarProperty Name="Id" ColumnName="Id" /> 
    </EndProperty> 
</AssociationSetMapping> 

並配有ScalarProperty添加一個EndPropertyDerivedDerived1Id喜歡這一點:

<AssociationSetMapping Name="FK_Items_Derived1" 
    TypeName="tempdatabase1Model1.FK_Items_Derived1" StoreEntitySet="Items"> 
    <EndProperty Name="Derived1"> 
     <ScalarProperty Name="Id" ColumnName="Derived1Id" /> 
    </EndProperty> 
    <EndProperty Name="Items"> 
     <ScalarProperty Name="Id" ColumnName="Id" /> 
    </EndProperty> 
</AssociationSetMapping> 

如果你更想做到這一點使用設計師(與我一樣),請執行以下操作:

  • 右鍵單擊Items和之間的關係。
  • 在上下文菜單中選擇「表格映射」。
  • 此關聯的映射詳細信息將顯示,您將立即看到其中一個列丟失。您可以從下拉列表中選擇Derived1Id uniqueidentifier,即可完成。
  • 請注意,如果Derive1實體具有其自己的外鍵屬性,您可能也不需要修正它們,但Entity Framework似乎交換了列。填寫缺失的列後,您可能需要交換兩列。
1

這裏是模型配置使用TPT映射。

<?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="tempdatabase1Model1.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2008" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"> 
     <EntityType Name="Base"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Name" Type="nvarchar" MaxLength="50" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Derived1"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Length" Type="int" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Derived2"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Size" Type="int" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Items"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Derived1Id" Type="uniqueidentifier" Nullable="false" /> 
      <Property Name="Description" Type="nvarchar" MaxLength="50" Nullable="false" /> 
     </EntityType> 
     <Association Name="FK_Derived1_Base"> 
      <End Role="Base" Type="Self.Base" Multiplicity="1" /> 
      <End Role="Derived1" Type="Self.Derived1" Multiplicity="0..1" /> 
      <ReferentialConstraint> 
      <Principal Role="Base"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Derived1"> 
       <PropertyRef Name="Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <Association Name="FK_Derived2_Base"> 
      <End Role="Base" Type="Self.Base" Multiplicity="1" /> 
      <End Role="Derived2" Type="Self.Derived2" Multiplicity="0..1" /> 
      <ReferentialConstraint> 
      <Principal Role="Base"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Derived2"> 
       <PropertyRef Name="Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <Association Name="FK_Items_Derived1"> 
      <End Role="Derived1" Type="Self.Derived1" Multiplicity="1" /> 
      <End Role="Items" Type="Self.Items" Multiplicity="*" /> 
      <ReferentialConstraint> 
      <Principal Role="Derived1"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Items"> 
       <PropertyRef Name="Derived1Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <EntityContainer Name="tempdatabase1Model1StoreContainer"> 
      <EntitySet Name="Base" EntityType="Self.Base" Schema="dbo" store:Type="Tables" /> 
      <EntitySet Name="Derived1" EntityType="Self.Derived1" Schema="dbo" store:Type="Tables" /> 
      <EntitySet Name="Derived2" EntityType="Self.Derived2" Schema="dbo" store:Type="Tables" /> 
      <EntitySet Name="Items" EntityType="Self.Items" Schema="dbo" store:Type="Tables" /> 
      <AssociationSet Name="FK_Derived1_Base" Association="Self.FK_Derived1_Base"> 
      <End Role="Base" EntitySet="Base" /> 
      <End Role="Derived1" EntitySet="Derived1" /> 
      </AssociationSet> 
      <AssociationSet Name="FK_Derived2_Base" Association="Self.FK_Derived2_Base"> 
      <End Role="Base" EntitySet="Base" /> 
      <End Role="Derived2" EntitySet="Derived2" /> 
      </AssociationSet> 
      <AssociationSet Name="FK_Items_Derived1" Association="Self.FK_Items_Derived1"> 
      <End Role="Derived1" EntitySet="Derived1" /> 
      <End Role="Items" EntitySet="Items" /> 
      </AssociationSet> 
     </EntityContainer> 
     </Schema> 
    </edmx:StorageModels> 
    <!-- CSDL content --> 
    <edmx:ConceptualModels> 
     <Schema Namespace="tempdatabase1Model1" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm"> 
     <EntityType Name="Base"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="Guid" Nullable="false" /> 
      <Property Name="Name" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Derived1" BaseType="tempdatabase1Model1.Base"> 
      <Property Name="Length" Type="Int32" Nullable="false" /> 
      <NavigationProperty Name="Items" Relationship="Self.FK_Items_Derived1" FromRole="Derived1" ToRole="Items" /> 
     </EntityType> 
     <EntityType Name="Derived2" BaseType="tempdatabase1Model1.Base"> 
      <Property Name="Size" Type="Int32" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Item"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="Guid" Nullable="false" /> 
      <Property Name="Description" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" /> 
      <NavigationProperty Name="Derived1" Relationship="Self.FK_Items_Derived1" FromRole="Items" ToRole="Derived1" /> 
     </EntityType> 
     <Association Name="FK_Derived1_Base"> 
      <End Role="Base" Type="Self.Base" Multiplicity="1" /> 
      <End Role="Derived1" Type="Self.Derived1" Multiplicity="0..1" /> 
      <ReferentialConstraint> 
      <Principal Role="Base"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Derived1"> 
       <PropertyRef Name="Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <Association Name="FK_Derived2_Base"> 
      <End Role="Base" Type="Self.Base" Multiplicity="1" /> 
      <End Role="Derived2" Type="Self.Derived2" Multiplicity="0..1" /> 
      <ReferentialConstraint> 
      <Principal Role="Base"> 
       <PropertyRef Name="Id" /> 
      </Principal> 
      <Dependent Role="Derived2"> 
       <PropertyRef Name="Id" /> 
      </Dependent> 
      </ReferentialConstraint> 
     </Association> 
     <Association Name="FK_Items_Derived1"> 
      <End Role="Derived1" Type="Self.Derived1" Multiplicity="1" /> 
      <End Role="Items" Type="Self.Item" Multiplicity="*" /> 
     </Association> 
     <EntityContainer Name="tempdatabase1Entities1" annotation:LazyLoadingEnabled="true"> 
      <EntitySet Name="Bases" EntityType="Self.Base" /> 
      <EntitySet Name="Items" EntityType="Self.Item" /> 
      <AssociationSet Name="FK_Derived1_Base" Association="Self.FK_Derived1_Base"> 
      <End Role="Base" EntitySet="Bases" /> 
      <End Role="Derived1" EntitySet="Bases" /> 
      </AssociationSet> 
      <AssociationSet Name="FK_Derived2_Base" Association="Self.FK_Derived2_Base"> 
      <End Role="Base" EntitySet="Bases" /> 
      <End Role="Derived2" EntitySet="Bases" /> 
      </AssociationSet> 
      <AssociationSet Name="FK_Items_Derived1" Association="Self.FK_Items_Derived1"> 
      <End Role="Derived1" EntitySet="Bases" /> 
      <End Role="Items" EntitySet="Items" /> 
      </AssociationSet> 
     </EntityContainer> 
     </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="tempdatabase1Model1StoreContainer" CdmEntityContainer="tempdatabase1Entities1"> 
      <EntitySetMapping Name="Bases"> 
      <EntityTypeMapping TypeName="IsTypeOf(tempdatabase1Model1.Base)"> 
       <MappingFragment StoreEntitySet="Base"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       <ScalarProperty Name="Name" ColumnName="Name" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      <EntityTypeMapping TypeName="IsTypeOf(tempdatabase1Model1.Derived1)"> 
       <MappingFragment StoreEntitySet="Derived1"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       <ScalarProperty Name="Length" ColumnName="Length" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      <EntityTypeMapping TypeName="IsTypeOf(tempdatabase1Model1.Derived2)"> 
       <MappingFragment StoreEntitySet="Derived2"> 
       <ScalarProperty Name="Size" ColumnName="Size" /> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      </EntitySetMapping> 
      <EntitySetMapping Name="Items"> 
      <EntityTypeMapping TypeName="tempdatabase1Model1.Item"> 
       <MappingFragment StoreEntitySet="Items"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       <ScalarProperty Name="Description" ColumnName="Description" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      </EntitySetMapping> 
      <AssociationSetMapping Name="FK_Items_Derived1" TypeName="tempdatabase1Model1.FK_Items_Derived1" StoreEntitySet="Items"> 
      <EndProperty Name="Derived1"> 
       <ScalarProperty Name="Id" ColumnName="Derived1Id" /> 
      </EndProperty> 
      <EndProperty Name="Items"> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
      </EndProperty> 
      </AssociationSetMapping> 
     </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="false" /> 
     <DesignerProperty Name="UseLegacyProvider" Value="true" /> 
     <DesignerProperty Name="CodeGenerationStrategy" Value="None" /> 
     </DesignerInfoPropertySet> 
    </Options> 
    <!-- Diagram content (shape and connector positions) --> 
    <Diagrams></Diagrams> 
    </Designer> 
</edmx:Edmx> 
1

一個實體框架代碼的第一個解決方案,它是值得的。

查看全部博客張貼在這裏:
http://jnye.co/Posts/18/table-per-type-tpt-database-using-entityframework-code-first

該機型

public abstract class BaseTable 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

[Table("DerivedWithRelation")] 
public class DerivedWithRelation : BaseTable 
{ 
    public int Amount { get; set; } 
    public string About { get; set; } 
    public int RelatedId { get; set; } 

    public virtual ICollection<Relation> Relations { get; set; } 
} 

[Table("DerivedWithoutRelation")] 
public class DerivedWithoutRelation : BaseTable 
{ 
    public int Quantity { get; set; } 
    public string Description { get; set; } 
} 

public class Relation 
{ 
    public int Id { get; set; } 
    public string RelationshipType { get; set; } 

    public virtual DerivedWithRelation DerivedWithRelation { get; set; } 
} 

上下文

public class MyContext : DbContext 
{ 
    public MyContext() 
     : base("DefaultConnection") 
    {    
    } 

    public IDbSet<BaseTable> BaseTables { get; set; } 
    public IDbSet<DerivedWithRelation> DerivedWithRelations { get; set; } 
    public IDbSet<DerivedWithoutRelation> DerivedWithoutRelations { get; set; } 
} 

Schema created using Entity Framework code first and TPT http://jnye.co/Content/Images/Demo/EntityFrameworkCodeFirstTPT.png

希望有所幫助。

相關問題