将 .Net 4 与 EF 1.0 一起使用:我有一个表,每个类型继承数据库结构和 edmx(参见文章末尾的图表和缩减 edmx。edmx 是通过代码生成器创建的)。我刚刚为这篇文章添加了一个子类型,但有很多!
我们所有的实体框架查询也是动态生成的。因此,当我尝试检索产品(可能是也可能不是一本书)时,我会生成以下 ESQL 查询。
SELECT VALUE [tb_product] FROM [tb_product] as [tb_product] WHERE (([tb_product].[Id] = @p0))
如果我请求的对象是一本书,那么我返回给我的对象是“tb_product”类型。我似乎无法从这个 tb_product 对象投射或以其他方式访问“书”信息。
如何修改查询、edmx 或其他一些组件,以便在查询基本类型时也可以访问所有相关的子类型信息,而无需再次访问数据库。我可以吗?
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="MyModel.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="MyModelStoreContainer">
<EntitySet Name="tb_productBook" EntityType="MyModel.Store.tb_productBook" store:Type="Tables" Schema="dbo" />
<EntitySet Name="tb_product" EntityType="MyModel.Store.tb_product" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_IdProductBook_Product" Association="MyModel.Store.FK_IdProductBook_Product">
<End Role="tb_product" EntitySet="tb_product" />
<End Role="tb_productBook" EntitySet="tb_productBook" />
</AssociationSet>
</EntityContainer>
<EntityType Name="tb_productBook">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="int" Nullable="false" />
<Property Name="title" Type="varchar" Nullable="false" MaxLength="150" />
<Property Name="subtitle" Type="varchar" Nullable="false" MaxLength="150" />
<Property Name="length" Type="int" Nullable="false" />
<Property Name="productCoverTypeId" Type="int" Nullable="false" />
<Property Name="volumeEdition" Type="varchar" Nullable="false" MaxLength="15" />
<Property Name="publisherName" Type="varchar" Nullable="false" MaxLength="100" />
<Property Name="publishDate" Type="datetime" />
<Property Name="iSBNNumber" Type="varchar" Nullable="false" MaxLength="30" />
<Property Name="description" Type="varchar" Nullable="false" />
<Property Name="authorBio" Type="varchar" Nullable="false" />
<Property Name="audience" Type="varchar" Nullable="false" />
<Property Name="expireDate" Type="datetime" />
<Property Name="vendorId" Type="int" Nullable="false" />
<Property Name="productCommissionTypeId" Type="int" Nullable="false" />
<Property Name="commissionValue" Type="decimal" Nullable="false" Precision="9" Scale="2" />
<Property Name="languageId" Type="int" Nullable="false" />
<Property Name="countryId" Type="int" Nullable="false" />
<Property Name="stateId" Type="int" Nullable="false" />
<Property Name="inventoryLocation" Type="varchar" Nullable="false" MaxLength="2000" />
</EntityType>
<EntityType Name="tb_product">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="name" Type="varchar" MaxLength="100" />
<Property Name="productSku" Type="varchar" Nullable="false" MaxLength="17" />
<Property Name="productTypeId" Type="int" Nullable="false" />
<Property Name="price" Type="decimal" Nullable="false" Precision="9" Scale="2" />
<Property Name="marketOnWeb" Type="bit" Nullable="false" />
<Property Name="subsidiaryId" Type="int" Nullable="false" />
<Property Name="lmsProductId" Type="int" />
<Property Name="topicId" Type="int" />
<Property Name="categoryId" Type="int" />
<Property Name="eventId" Type="int" />
<Property Name="note" Type="varchar" />
<Property Name="expirationDate" Type="datetime" />
<Property Name="mediaLength" Type="int" />
<Property Name="urlSlug" Type="varchar" MaxLength="200" />
<Property Name="productSourceId" Type="int" />
<Property Name="created" Type="datetime" Nullable="false" />
<Property Name="createdBy" Type="varchar" Nullable="false" MaxLength="30" />
<Property Name="lastUpdated" Type="datetime" Nullable="false" />
<Property Name="lastUpdatedBy" Type="varchar" Nullable="false" MaxLength="30" />
</EntityType>
<Association Name="FK_IdProductBook_Product">
<End Role="tb_product" Type="MyModel.Store.tb_product" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="tb_productBook" Type="MyModel.Store.tb_productBook" Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="tb_product">
<PropertyRef Name="id" />
</Principal>
<Dependent Role="tb_productBook">
<PropertyRef Name="id" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="MyEntities">
<EntitySet Name="tb_product" EntityType="MyModel.tb_product" />
</EntityContainer>
<EntityType Name="tb_productBook" BaseType="MyModel.tb_product">
<Property Name="title" Type="String" Nullable="false" />
<Property Name="subtitle" Type="String" Nullable="false" />
<Property Name="length" Type="Int32" Nullable="false" />
<Property Name="volumeEdition" Type="String" Nullable="false" />
<Property Name="publisherName" Type="String" Nullable="false" />
<Property Name="publishDate" Type="DateTime" Nullable="true" />
<Property Name="iSBNNumber" Type="String" Nullable="false" />
<Property Name="description" Type="String" Nullable="false" />
<Property Name="authorBio" Type="String" Nullable="false" />
<Property Name="audience" Type="String" Nullable="false" />
<Property Name="expireDate" Type="DateTime" Nullable="true" />
<Property Name="vendorId" Type="Int32" Nullable="false" />
<Property Name="commissionValue" Type="Decimal" Nullable="false" />
<Property Name="inventoryLocation" Type="String" Nullable="false" />
<NavigationProperty Name="ProductBook_Country" Relationship="MyModel.FK_CountryProductBook_Country" FromRole="tb_productBook" ToRole="tb_country" />
<NavigationProperty Name="ProductBook_Language" Relationship="MyModel.FK_LanguageProductBook_Language" FromRole="tb_productBook" ToRole="tb_language" />
<NavigationProperty Name="ProductBook_ProductCommissionType" Relationship="MyModel.FK_ProductCommissionTypeProductBook_ProductCommissionType" FromRole="tb_productBook" ToRole="tb_productCommissionType" />
<NavigationProperty Name="ProductBook_ProductCoverType" Relationship="MyModel.FK_ProductCoverTypeProductBook_ProductCoverType" FromRole="tb_productBook" ToRole="tb_productCoverType" />
<NavigationProperty Name="ProductBook_State" Relationship="MyModel.FK_StateProductBook_State" FromRole="tb_productBook" ToRole="tb_state" />
</EntityType>
<EntityType Name="tb_product">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Int32" Nullable="false" />
<Property Name="name" Type="String" Nullable="true" />
<Property Name="productSku" Type="String" Nullable="false" />
<Property Name="price" Type="Decimal" Nullable="false" />
<Property Name="marketOnWeb" Type="Boolean" Nullable="false" />
<Property Name="subsidiaryId" Type="Int32" Nullable="false" />
<Property Name="lmsProductId" Type="Int32" Nullable="true" />
<Property Name="topicId" Type="Int32" Nullable="true" />
<Property Name="note" Type="String" Nullable="true" />
<Property Name="expirationDate" Type="DateTime" Nullable="true" />
<Property Name="mediaLength" Type="Int32" Nullable="true" />
<Property Name="urlSlug" Type="String" Nullable="true" />
<Property Name="created" Type="DateTime" Nullable="false" />
<Property Name="createdBy" Type="String" Nullable="false" />
<Property Name="lastUpdated" Type="DateTime" Nullable="false" />
<Property Name="lastUpdatedBy" Type="String" Nullable="false" />
<NavigationProperty Name="CampaignAdvertisedProduct_Product" Relationship="MyModel.FK_ProductCampaignAdvertisedProduct_Product" FromRole="tb_product" ToRole="tb_campaignAdvertisedProduct" />
<NavigationProperty Name="DiscountGroupProduct_Product" Relationship="MyModel.FK_ProductDiscountGroupProduct_Product" FromRole="tb_product" ToRole="tb_discountGroupProduct" />
<NavigationProperty Name="LogCampaignResponseUpdate_Product" Relationship="MyModel.FK_ProductLogCampaignResponseUpdate_Product" FromRole="tb_product" ToRole="tb_logCampaignResponseUpdate" />
<NavigationProperty Name="ProductAuthor_Product" Relationship="MyModel.FK_ProductProductAuthor_Product" FromRole="tb_product" ToRole="tb_productAuthor" />
<NavigationProperty Name="ProductImage_Product" Relationship="MyModel.FK_ProductProductImage_Product" FromRole="tb_product" ToRole="tb_productImage" />
<NavigationProperty Name="ProductMarketingContent_Product" Relationship="MyModel.FK_ProductProductMarketingContent_Product" FromRole="tb_product" ToRole="tb_productMarketingContent" />
<NavigationProperty Name="ProductOnsiteStaffAgency_Product" Relationship="MyModel.FK_ProductProductOnsiteStaffAgency_Product" FromRole="tb_product" ToRole="tb_productOnsiteStaffAgency" />
<NavigationProperty Name="ProductProblem_Product" Relationship="MyModel.FK_ProductProductProblem_Product" FromRole="tb_product" ToRole="tb_productProblem" />
<NavigationProperty Name="ProductVenue_Product" Relationship="MyModel.FK_ProductProductVenue_Product" FromRole="tb_product" ToRole="tb_productVenue" />
<NavigationProperty Name="VenueEmailRequest_Product" Relationship="MyModel.FK_ProductVenueEmailRequest_Product" FromRole="tb_product" ToRole="tb_venueEmailRequest" />
<NavigationProperty Name="Product_Category" Relationship="MyModel.FK_CategoryProduct_Category" FromRole="tb_product" ToRole="tb_category" />
<NavigationProperty Name="Product_ProductSource" Relationship="MyModel.FK_ProductSourceProduct_ProductSource" FromRole="tb_product" ToRole="tb_productSource" />
<NavigationProperty Name="Product_ProductType" Relationship="MyModel.FK_ProductTypeProduct_ProductType" FromRole="tb_product" ToRole="tb_productType" />
<NavigationProperty Name="Product_Event" Relationship="MyModel.FK_EventProduct_Event" FromRole="tb_product" ToRole="tb_event" />
<NavigationProperty Name="tb_category" Relationship="MyModel.tb_product_category" FromRole="tb_product" ToRole="tb_category" />
<NavigationProperty Name="tb_contact" Relationship="MyModel.tb_product_faculty" FromRole="tb_product" ToRole="tb_contact" />
<NavigationProperty Name="tb_state" Relationship="MyModel.tb_product_State" FromRole="tb_product" ToRole="tb_state" />
<NavigationProperty Name="tb_vendorCost" Relationship="MyModel.tb_product_VendorCost" FromRole="tb_product" ToRole="tb_vendorCost" />
<NavigationProperty Name="tb_vendorInvoice" Relationship="MyModel.tb_product_VendorInvoice" FromRole="tb_product" ToRole="tb_vendorInvoice" />
<NavigationProperty Name="tb_vendorPayment" Relationship="MyModel.tb_product_VendorPayment" FromRole="tb_product" ToRole="tb_vendorPayment" />
</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="MyModelStoreContainer" CdmEntityContainer="MyEntities">
<EntitySetMapping Name="tb_product">
<EntityTypeMapping TypeName="IsTypeOf(MyModel.tb_product)">
<MappingFragment StoreEntitySet="tb_product">
<ScalarProperty Name="lastUpdatedBy" ColumnName="lastUpdatedBy" />
<ScalarProperty Name="lastUpdated" ColumnName="lastUpdated" />
<ScalarProperty Name="createdBy" ColumnName="createdBy" />
<ScalarProperty Name="created" ColumnName="created" />
<ScalarProperty Name="urlSlug" ColumnName="urlSlug" />
<ScalarProperty Name="mediaLength" ColumnName="mediaLength" />
<ScalarProperty Name="expirationDate" ColumnName="expirationDate" />
<ScalarProperty Name="note" ColumnName="note" />
<ScalarProperty Name="topicId" ColumnName="topicId" />
<ScalarProperty Name="lmsProductId" ColumnName="lmsProductId" />
<ScalarProperty Name="subsidiaryId" ColumnName="subsidiaryId" />
<ScalarProperty Name="marketOnWeb" ColumnName="marketOnWeb" />
<ScalarProperty Name="price" ColumnName="price" />
<ScalarProperty Name="productSku" ColumnName="productSku" />
<ScalarProperty Name="name" ColumnName="name" />
<ScalarProperty Name="id" ColumnName="id" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(MyModel.tb_productBook)">
<MappingFragment StoreEntitySet="tb_productBook">
<ScalarProperty Name="inventoryLocation" ColumnName="inventoryLocation" />
<ScalarProperty Name="commissionValue" ColumnName="commissionValue" />
<ScalarProperty Name="vendorId" ColumnName="vendorId" />
<ScalarProperty Name="expireDate" ColumnName="expireDate" />
<ScalarProperty Name="audience" ColumnName="audience" />
<ScalarProperty Name="authorBio" ColumnName="authorBio" />
<ScalarProperty Name="description" ColumnName="description" />
<ScalarProperty Name="iSBNNumber" ColumnName="iSBNNumber" />
<ScalarProperty Name="publishDate" ColumnName="publishDate" />
<ScalarProperty Name="publisherName" ColumnName="publisherName" />
<ScalarProperty Name="volumeEdition" ColumnName="volumeEdition" />
<ScalarProperty Name="length" ColumnName="length" />
<ScalarProperty Name="subtitle" ColumnName="subtitle" />
<ScalarProperty Name="title" ColumnName="title" />
<ScalarProperty Name="id" ColumnName="id" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>