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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:59:57+00:00 2026-05-12T22:59:57+00:00

Short Question: Basically I am trying to add a scalar property to my entity

  • 0

Short Question:

Basically I am trying to add a scalar property to my entity which holds the ID of a FK entity.

What I have tried to do thus far:

What I have tried so far is adding the scalar property (called ChildId) and mapped it to the matching column in the database. Now as you can imagine I get some exceptions when I try and do this because entity framework complains that the FK id is being managed in two places, once through x.ChildId and the other through x.Child.ChildId.

Now I get why it is doing this but I need some why to be able to have a scalar property which is automatically populated with the ChildId.

What I know I could do but really don’t want to:

I realize that I could write a linq query that does something like the following (where I have implemented the other half of the partial class and added a property there called ChildId):

from x in db.Parent
select new Parent { ParentName = x.ParentName, ..., ChildId = x.Child.ChildId }

But this is extremely messy, particular when I have 30 odd queries that return a parent object, this mapping would need to be repeated for each query…

Also I realize that after I have executed the query I could go something like:

var childId = parent.Child.Id;

But this would cause either an extra query to be triggered, or if I was proactively loading child, and in either case I would be pulling out a lot more data than I need when I only want the ID…

The required end result:

So how do I get around some of these limitations so that I can write my queries like so (or something very similar):

from x in db.Parent
select x

And have it so that I can either go:

var childId = parent.Child.Id;   //Where in this case the only property retrieved would be the Id 
//Or
var childId = parent.ChildId;

Cheers
Anthony

EDIT:

Hey thanks for the reply…

I just figured this out for myself as well. Basically I was thinking that if EF supports lazy loading it must be storing the ID somewhere. Then it clicked that it must be in the reference… Hence for me it worked out being something like:

destination.PlanTypeId = (int)source.PlanTypeReference.EntityKey.EntityKeyValues[0].Value; 

Also thanks for the idea of creating the extension property… will be very useful.

  • 1 1 Answer
  • 1 View
  • 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-12T22:59:58+00:00Added an answer on May 12, 2026 at 10:59 pm

    Did you try using parent.ChildReference.EntityKey? It doesn’t need to be additionaly loaded and is holding FK. You can write extension method to get key easier.

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

Sidebar

Related Questions

SHORT VERSION OF QUESTION: So basically my question is: How can I set the
This is basically a rails 3 version of this question . Short of parsing
Short question: Is the parent of an entity group included in that entity group
Short question: I'm trying to make that in a given page (uses tabs) back
I am trying to make a range control which is basically a slider control
Basically, my question is short and sweet: Is the following a bad idea (encapsulating
Basically, I have a small issue trying to display each attibute seperetly when I
Good day, I have a practical mysql question to which I can't find help
Short question: Do any of MS's built in Data Objects support INotifyPropertyChanged? Long explination:
Short question - how do you define your view models? Here are some of

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.