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 3699258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:04:52+00:00 2026-05-19T05:04:52+00:00

If i have SQL Server tables like this: Location ———- LocationId int PK Field1

  • 0

If i have SQL Server tables like this:

Location
----------
LocationId int PK 
Field1 int
Field2 int
etc

Score
------------------------------------
LocationId int PK, FK
IsSingleLevel bit PK (discriminator)
Field1 int
Field2 int
etc

Technically this is mapped as a Location 1..0..* Score, but with the PK of LocationId/IsSingleLevel, it’s a Location 1..0..2.

When i drag this on the EDMX, set everything up as expected (abstract entity, remove discriminator from base entity, etc).

EF give this error three times (one for base entity, and one for the two derived entities):

Error 6 Error 3025: Problem in mapping fragments starting at line 2146:Must specify mapping for all key properties (LocationScore.LocationId, LocationScore.IsSingleLevelScore) of table LocationScore.

I followed the example here.

The error occurs because i have the discriminator as part of the PK in the database, and discriminators are not mapped on the model, so i get the error.

I can’t have LocationId as the only field in the PK, because then a location could only have 1 score, i need it to have two scores (one single level, one overall).

The end result is i want to be able to do this:

Locations.Include("LocationOverallScore").Single();
Locations.Include("LocationSingleLevelScore").Single();

Where LocationOverallScore and LocationSingleLevelScore are derived entities from the LocationScore base (abstract entity).

Is TPH not the right design here? Is my database design wrong? The aim is i don’t want to have 2 physical tables for the different scores – as the table is huge, and i don’t want to repeat the columns.

There’s two possible workarounds i can think of:

1 – Make a view (LocationScore), which UNION’s the two tables (so it would return 2 rows per LocationId) – but i still don’t think i can “TPH” this. I don’t want to manually perform the JOIN, i want to eager load.

2 – Add an IDENTITY column to Score, and this can be the PK.

Can you guys think of another solution?

  • 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-05-19T05:04:52+00:00Added an answer on May 19, 2026 at 5:04 am

    I found a solution. I’m not 100% happy with it, but it does satisfy my requirement of not having two tables.

    In the database side:

    LocationScore:

    • LocationScoreId: IDENTITY, PK
    • LocationId: FK
    • Unique Index on LocationId/IsSingleLevelScore

    I imported that into my EDMX – and included the LocationId FK (i never usually do this – but it’s required in this instance).

    Created the derived entities, mapped the fields, set the discriminator.

    Created an association between Location -> LocationOverallScore, Location -> LocationScore (based on LocationId referential constraint).

    All works fine.

    The one downside is that because LocationId is not part of the PK on the LocationScore table, it’s a 1..* between Location and LocationOverallScore, when in reality it should only be a 1..1.

    I enforce this business requirement in the model via a hook property:

    public class Location
    {
       // EF navigational properties - required
       public ICollection<LocationOverallScore> LocationOverallScores { get; set; }
       public ICollection<LocationSingleLevelScore> LocationSingleLevelScores { get; set; }
    
       // Hook properties
       public LocationOverallScore OverallScore
       {
          get { return LocationOverallScores.SingleOrDefault(); }
       }
       public LocationSingleLevelScores SingleLevelScore
       {
          get { return LocationSingleLevelScores .SingleOrDefault(); }
       }
    }
    

    So the .SingleOrDefault() will throw an exception if there is more than one record – which there never will be, because of the unique index.

    So i can now do this:

    var location = ctx.Locations.Include("LocationOverallScores").Single();
    var overallScore = location.OverallScore;
    

    That’s what i’ll be going with for now.

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

Sidebar

Related Questions

I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
I have to write a component that re-creates SQL Server tables (structure and data)
I have a SQL Server 2000 database with around a couple of hundred tables.
I have designed database tables (normalised, on an MS SQL server) and created a
I have a couple of tables in a SQL 2008 server that I need
We have a SQL Server table containing Company Name, Address, and Contact name (among
I have an SQL Server DB with a table with these fields: A bit
I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.
I have the following table and data in SQL Server 2005: create table LogEntries
I have created a foreign key (in SQL Server) by: alter table company add

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.