Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:44:54+00:00 2026-06-02T01:44:54+00:00

My question relates to the table joining function in the ADO.NET Entity Framework. Imagine

  • 0

My question relates to the “table joining” function in the ADO.NET Entity Framework.
Imagine a database that contains a table “Product” and another table “ProductPrice”. The price table stores a history of all price changes for a product, with a start and end date, where the line containing the current price is indicated by a NULL value in the end date column. This database structure could be useful for statistical purposes, for example the average daily sales volume could be mapped to each change in the product price. However, for the online ordering website, only the current price is required.

Here’s the structure of the two tables:

Product
ProductID (PK, int, NOT NULL, auto increment)
Name (varchar 50, NOT NULL)

ProductPrice
ProductPriceID (PK, int, NOT NULL, auto increment)
ProductID (INT, NOT NULL)
StartDate (DATETIME, NOT NULL)
EndDate (DATETIME)
Price (MONEY, NOT NULL)

Here’s an example of an SQL statement to retrieve the product plus the current price:

SELECT Product.ProductID, Product.Name, ProductPrice.Price AS CurrentPrice
FROM Product
LEFT JOIN ProductPrice
ON Product.ProductID = ProductPrice.ProductID
AND ProductPrice.EndDate IS NULL

I’d like to use the Entity Framework to join the entities Product and ProductPrice together, so that I can access the current price directly from the Product entity, as in the following example:

var product = (from p in context.Product where p.ProductID == 2 select p).FirstOrDefault();
Console.WriteLine(product.Name);
Console.WriteLine(product.CurrentPrice);

Unfortunately, I’m getting stuck with errors that I can’t resolve.

Here are the entities from the storage model:

<EntityType Name="Product">
  <Key>
    <PropertyRef Name="ProductID" />
  </Key>
  <Property Name="ProductID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
</EntityType>
<EntityType Name="ProductPrice">
  <Key>
    <PropertyRef Name="ProductPriceID" />
  </Key>
  <Property Name="ProductPriceID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="ProductID" Type="int" Nullable="false" />
  <Property Name="Price" Type="money" Nullable="false" />
  <Property Name="StartDate" Type="datetime" Nullable="false" />
  <Property Name="EndDate" Type="datetime" />
</EntityType>
<Association Name="FK_ProductPrice_Product">
  <End Role="Product" Type="TestingModel.Store.Product" Multiplicity="1" />
  <End Role="ProductPrice" Type="TestingModel.Store.ProductPrice" Multiplicity="*" />
  <ReferentialConstraint>
    <Principal Role="Product">
      <PropertyRef Name="ProductID" />
    </Principal>
    <Dependent Role="ProductPrice">
      <PropertyRef Name="ProductID" />
    </Dependent>
  </ReferentialConstraint>
</Association>

And from the conceptual model:

<EntityType Name="Product">
  <Key>
    <PropertyRef Name="ProductID" />
  </Key>
  <Property Name="ProductID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
  <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
  <Property Name="SKU" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
  <Property Type="Decimal" Name="CurrentPrice" Nullable="false" Precision="19" Scale="4" />
</EntityType>

And finally the mapping between the two:

<EntitySetMapping Name="Product">
  <EntityTypeMapping TypeName="TestingModel.Product">
    <MappingFragment StoreEntitySet="Product">
      <ScalarProperty Name="ProductID" ColumnName="ProductID" />
      <ScalarProperty Name="Name" ColumnName="Name" />
      <ScalarProperty Name="SKU" ColumnName="SKU" />
    </MappingFragment>
  </EntityTypeMapping>
  <EntityTypeMapping TypeName="IsTypeOf(TestingModel.Product)">
    <MappingFragment StoreEntitySet="ProductPrice">
      <ScalarProperty Name="CurrentPrice" ColumnName="Price" />
      <Condition ColumnName="EndDate" IsNull="true" />
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>

And here are the error messages that I’m currently struggling with:

Error   1   Error 3024: Problem in mapping fragments starting at line 76:Must specify mapping for all key properties (Product.ProductID) of the EntitySet Product.
Error   2   Error 3025: Problem in mapping fragments starting at line 76:Must specify mapping for all key properties (ProductPrice.ProductPriceID) of table ProductPrice.

I’m not sure if this is even possible with the Entity Framework, maybe I should just do the join manually myself in LINQ.

Any suggestions would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:44:55+00:00Added an answer on June 2, 2026 at 1:44 am

    You do not meet the conditions for entity splitting:

    You should only map an entity type to multiple tables if the following conditions are true:

    • The tables to which you are mapping share a common key.

    • The entity type that is being mapped has entries in each underlying table. In other words, the entity type represents data that has a one-to-one correspondence between the two tables; the entity type represents an inner join of the two tables.

    You are smarter than EF. You know that the condition EndDate == null yields one record. EF has no clue to know that and thus never knows that it can create one object from the two tables. In terms of the exception message: PriceId and EndDate == null should somehow deliver all key properties of your ProductPrice records, which is impossible, obviously.

    Alternatives:

    A. You can create a one-to-many association between the two and query:

    products.Where(p => p.ProductId == 2)
        .Select(p => new
          {
            Product = p,
            CurrentPrice = p.ProductPrices.Where(pp => pp.EndDate == null)
                           .FirstOrDefault()
          })
    

    Not as convenient as you’d like, but maybe not too bad when wrapped in a repository.

    B. Create a database view and use separate paths for viewing and updating (which is not an uncommon thing to do).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that relates to setting up the third table ( Repair_Parts
This relates to my other question on accessing a REST service that uses forms
Another question related to this one . I have a List<SortableObjects> that is the
My question relates to identifying which table and schema a mysql command is executed
My question relates to the innards of how Postgres works: I have a table:
My question relates to the best practices design of tables in a database for
question relates to PHP and Javascript for now every table has a form with
this time i have a simple question: I have one table, related with himself
This question relates to this question which I asked earlier this week. The answer
This question relates to those parts of the KenKen Latin Square puzzles which ask

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.