.NET 4.5 で Entity Framework 5.0 を使用しています。サブクラスの 1 つの主キーへの外部キーがある Type Per Type (TPT) 継承階層を構築しようとしています。残念ながら、Entity Framework で次のコンパイル エラーが発生します。
エラー 3024: 163 行目から始まるフラグメントのマッピングに問題があります: Relationship FK_Items_Derived1 の End Derived1 のすべてのキー プロパティ (Id) のマッピングを指定する必要があります。
モデルのデモを示すために、テスト データベースと EF モデルを作成しました。私のデータベースモデルは次のようになります。
Entity Framework モデルを作成するには、次のようにします。
- これらのテーブルを Entity Framework デザイナーに追加しました。
- と
Base
のベース タイプとして選択されています。Derived1
Derived2
Derived1
and とandBase
の間の外部キー関係を削除しました(現在は継承関係があるため)。Derived2
Base
- と
Id
からプロパティを削除しました(プロパティを から継承しているため)。Derived1
Derived2
Id
Base
その結果が次のモデルです。
これをコンパイルすると、Entity Framework によって前述のコンパイル エラーが表示されます。
エラー 3024: 163 行目から始まるフラグメントのマッピングに問題があります: Relationship FK_Items_Derived1 の End Derived1 のすべてのキー プロパティ (Id) のマッピングを指定する必要があります。
エラーは、モデルの 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
だけがあり、 はありません。Entity Framework デザイナーがこのかなり一般的なユース ケースを処理できない理由がわかりません。Derived1
Derived2
もちろん、問題はこれをどのように修正するかです。
完全にするために、完全な Entity Framework マッピング ファイルを次に示します。
<?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