Problems with inheritance query view and one to many association in entity framework 4
- by Kazys
Hi,
I have situation in with I stucked and don't know way out. The problem is in my bigger model, but I have made small example which shows the same problem. I have 4 tables. I called them SuperParent, NamedParent, TypedParent and ParentType.
NamedParent and TypedParent derives from superParent. TypedParent has one to many association with ParentType. I describe mapping for entities using queryView. The problem is then I want to get TypedParents and Include ParentType I get the following exception:
An error occurred while preparing the command definition. See the inner exception for details. --- System.ArgumentException: The ResultType of the specified expression is not compatible with the required type. The expression ResultType is 'Transient.reference[PasibandymaiModel.SuperParent]' but the required type is 'Transient.reference[PasibandymaiModel.TypedParent]'.
Parameter name: arguments[1]
To get TypedParents I use following code:
context.SuperParent.OfType().Include("ParentType");
my edmx file:
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="PasibandymaiModel.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/2009/02/edm/ssdl">
<EntityContainer Name="PasibandymaiModelStoreContainer">
<EntitySet Name="NamedParent" EntityType="PasibandymaiModel.Store.NamedParent" store:Type="Tables" Schema="dbo" />
<EntitySet Name="ParentType" EntityType="PasibandymaiModel.Store.ParentType" store:Type="Tables" Schema="dbo" />
<EntitySet Name="SuperParent" EntityType="PasibandymaiModel.Store.SuperParent" store:Type="Tables" Schema="dbo" />
<EntitySet Name="TypedParent" EntityType="PasibandymaiModel.Store.TypedParent" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="fk_NamedParent_SuperParent" Association="PasibandymaiModel.Store.fk_NamedParent_SuperParent">
<End Role="SuperParent" EntitySet="SuperParent" />
<End Role="NamedParent" EntitySet="NamedParent" />
</AssociationSet>
<AssociationSet Name="fk_TypedParent_ParentType" Association="PasibandymaiModel.Store.fk_TypedParent_ParentType">
<End Role="ParentType" EntitySet="ParentType" />
<End Role="TypedParent" EntitySet="TypedParent" />
</AssociationSet>
<AssociationSet Name="fk_TypedParent_SuperParent" Association="PasibandymaiModel.Store.fk_TypedParent_SuperParent">
<End Role="SuperParent" EntitySet="SuperParent" />
<End Role="TypedParent" EntitySet="TypedParent" />
</AssociationSet>
</EntityContainer>
<EntityType Name="NamedParent">
<Key>
<PropertyRef Name="ParentId" />
</Key>
<Property Name="ParentId" Type="int" Nullable="false" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="100" />
</EntityType>
<EntityType Name="ParentType">
<Key>
<PropertyRef Name="ParentTypeId" />
</Key>
<Property Name="ParentTypeId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="nvarchar" MaxLength="100" />
</EntityType>
<EntityType Name="SuperParent">
<Key>
<PropertyRef Name="ParentId" />
</Key>
<Property Name="ParentId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="SomeAttribute" Type="nvarchar" Nullable="false" MaxLength="100" />
</EntityType>
<EntityType Name="TypedParent">
<Key>
<PropertyRef Name="ParentId" />
</Key>
<Property Name="ParentId" Type="int" Nullable="false" />
<Property Name="ParentTypeId" Type="int" Nullable="false"/>
</EntityType>
<Association Name="fk_NamedParent_SuperParent">
<End Role="SuperParent" Type="PasibandymaiModel.Store.SuperParent" Multiplicity="1" />
<End Role="NamedParent" Type="PasibandymaiModel.Store.NamedParent" Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="SuperParent">
<PropertyRef Name="ParentId" />
</Principal>
<Dependent Role="NamedParent">
<PropertyRef Name="ParentId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="fk_TypedParent_ParentType">
<End Role="ParentType" Type="PasibandymaiModel.Store.ParentType" Multiplicity="1" />
<End Role="TypedParent" Type="PasibandymaiModel.Store.TypedParent" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="ParentType">
<PropertyRef Name="ParentTypeId" />
</Principal>
<Dependent Role="TypedParent">
<PropertyRef Name="ParentTypeId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="fk_TypedParent_SuperParent">
<End Role="SuperParent" Type="PasibandymaiModel.Store.SuperParent" Multiplicity="1" />
<End Role="TypedParent" Type="PasibandymaiModel.Store.TypedParent" Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="SuperParent">
<PropertyRef Name="ParentId" />
</Principal>
<Dependent Role="TypedParent">
<PropertyRef Name="ParentId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Function Name="ChildDelete" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ChildId" Type="int" Mode="In" />
</Function>
<Function Name="ChildInsert" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Name" Type="nvarchar" Mode="In" />
<Parameter Name="ParentId" Type="int" Mode="In" />
</Function>
<Function Name="ChildUpdate" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ChildId" Type="int" Mode="In" />
<Parameter Name="ParentId" Type="int" Mode="In" />
<Parameter Name="Name" Type="nvarchar" Mode="In" />
</Function>
<Function Name="NamedParentDelete" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentId" Type="int" Mode="In" />
</Function>
<Function Name="NamedParentInsert" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Name" Type="nvarchar" Mode="In" />
<Parameter Name="SomeAttribute" Type="nvarchar" Mode="In" />
</Function>
<Function Name="NamedParentUpdate" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentId" Type="int" Mode="In" />
<Parameter Name="SomeAttribute" Type="nvarchar" Mode="In" />
<Parameter Name="Name" Type="nvarchar" Mode="In" />
</Function>
<Function Name="ParentTypeDelete" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentTypeId" Type="int" Mode="In" />
</Function>
<Function Name="ParentTypeInsert" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Name" Type="nvarchar" Mode="In" />
</Function>
<Function Name="ParentTypeUpdate" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentTypeId" Type="int" Mode="In" />
<Parameter Name="Name" Type="nvarchar" Mode="In" />
</Function>
<Function Name="TypedParentDelete" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentId" Type="int" Mode="In" />
</Function>
<Function Name="TypedParentInsert" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentTypeId" Type="int" Mode="In" />
<Parameter Name="SomeAttribute" Type="nvarchar" Mode="In" />
</Function>
<Function Name="TypedParentUpdate" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="ParentId" Type="int" Mode="In" />
<Parameter Name="SomeAttribute" Type="nvarchar" Mode="In" />
<Parameter Name="ParentTypeId" Type="int" Mode="In" />
</Function>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="PasibandymaiModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="PasibandymaiEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="ParentType" EntityType="PasibandymaiModel.ParentType" />
<EntitySet Name="SuperParent" EntityType="PasibandymaiModel.SuperParent" />
<AssociationSet Name="ParentTypeTypedParent" Association="PasibandymaiModel.ParentTypeTypedParent">
<End Role="ParentType" EntitySet="ParentType" />
<End Role="TypedParent" EntitySet="SuperParent" />
</AssociationSet>
</EntityContainer>
<EntityType Name="NamedParent" BaseType="PasibandymaiModel.SuperParent">
<Property Type="String" Name="Name" Nullable="false" MaxLength="100" FixedLength="false" Unicode="true" />
</EntityType>
<EntityType Name="ParentType">
<Key>
<PropertyRef Name="ParentTypeId" />
</Key>
<Property Type="Int32" Name="ParentTypeId" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" MaxLength="100" FixedLength="false" Unicode="true" />
<NavigationProperty Name="TypedParent" Relationship="PasibandymaiModel.ParentTypeTypedParent" FromRole="ParentType" ToRole="TypedParent" />
</EntityType>
<EntityType Name="SuperParent" Abstract="true">
<Key>
<PropertyRef Name="ParentId" />
</Key>
<Property Type="Int32" Name="ParentId" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="SomeAttribute" Nullable="false" MaxLength="100" FixedLength="false" Unicode="true" />
</EntityType>
<EntityType Name="TypedParent" BaseType="PasibandymaiModel.SuperParent">
<NavigationProperty Name="ParentType" Relationship="PasibandymaiModel.ParentTypeTypedParent" FromRole="TypedParent" ToRole="ParentType" />
<Property Type="Int32" Name="ParentTypeId" Nullable="false" />
</EntityType>
<Association Name="ParentTypeTypedParent">
<End Type="PasibandymaiModel.ParentType" Role="ParentType" Multiplicity="1" />
<End Type="PasibandymaiModel.TypedParent" Role="TypedParent" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="ParentType">
<PropertyRef Name="ParentTypeId" />
</Principal>
<Dependent Role="TypedParent">
<PropertyRef Name="ParentTypeId" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="PasibandymaiModelStoreContainer" CdmEntityContainer="PasibandymaiEntities">
<EntitySetMapping Name="ParentType">
<QueryView>
SELECT VALUE
PasibandymaiModel.ParentType(tp.ParentTypeId, tp.Name)
FROM PasibandymaiModelStoreContainer.ParentType AS tp
</QueryView>
</EntitySetMapping>
<EntitySetMapping Name="SuperParent">
<QueryView>
SELECT VALUE
CASE
WHEN (np.ParentId IS NOT NULL) THEN PasibandymaiModel.NamedParent(sp.ParentId, sp.SomeAttribute, np.Name)
WHEN (tp.ParentId IS NOT NULL) THEN PasibandymaiModel.TypedParent(sp.ParentId, sp.SomeAttribute, tp.ParentTypeId)
END
FROM PasibandymaiModelStoreContainer.SuperParent AS sp
LEFT JOIN PasibandymaiModelStoreContainer.NamedParent AS np ON sp.ParentId = np.ParentId
LEFT JOIN PasibandymaiModelStoreContainer.TypedParent AS tp ON sp.ParentId = tp.ParentId
</QueryView>
<QueryView TypeName="PasibandymaiModel.TypedParent">
SELECT VALUE
PasibandymaiModel.TypedParent(sp.ParentId, sp.SomeAttribute, tp.ParentTypeId)
FROM PasibandymaiModelStoreContainer.SuperParent AS sp
INNER JOIN PasibandymaiModelStoreContainer.TypedParent AS tp ON sp.ParentId = tp.ParentId
</QueryView>
<QueryView TypeName="PasibandymaiModel.NamedParent">
SELECT VALUE
PasibandymaiModel.NamedParent(sp.ParentId, sp.SomeAttribute, np.Name)
FROM PasibandymaiModelStoreContainer.SuperParent AS sp
INNER JOIN PasibandymaiModelStoreContainer.NamedParent AS np ON sp.ParentId = np.ParentId
</QueryView>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
</edmx:Edmx>
I have tried using AssociationSetMapping instead of using Association with ReferentialConstraint. But then couldn't insert related entities at once, becouse entity framework didn't provided entity key of inserted entities for related entities.
Thanks for any idea