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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:58:18+00:00 2026-06-05T13:58:18+00:00

I have a lookup table and a data table in my db. I’ll use

  • 0

I have a lookup table and a data table in my db. I’ll use gender and person for an example. So let’s say the gender table looks like so:

Id         Code
1          Male
2          Female

and the person table looks like so:

Id         Name             GenderId
1          Bob              1
2          Jane             2

I’ve modelled both tables in EF code first like so:

public class Gender
{
    public int Id {get;set;}
    public string Code {get;set;}
}

public class Person
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int GenderId {get;set;}

    public virtual Gender {get;set;}
}

If I read a person already in the DB then I can access person.Gender.Code without a problem. If I do this:

var person = new Person
             {
                 Name = "Bob",
                 GenderId = 1,
             };

context.People.Add(person);
context.SaveChanges();

var code = person.Gender.Code;

Then it will save correctly but will fail on the last line as gender is null. If I then open a new context and load the saved entity then the last line works fine. Is there a way that I can access gender directly after a save as if I just loaded the entity from the DB?

  • 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-05T13:58:20+00:00Added an answer on June 5, 2026 at 1:58 pm

    Your problem is that when you use new Person() it will just create a POCO object which doesn’t know how to get the it’s Gender property. So to make the lazy loading work you need proxies.

    You can create your person as a proxy with DbSet.Create():

    var person = context.People.Create();
    person.Name = "Bob";
    person.GenderId = 1;
    
    context.People.Add(person);
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a lookup table which I'd like to make accessible to
How can I model a mutable lookup table using Core Data? Say I have
I am trying to code a global lookup table of sorts. I have game
I have a 2D lookup table of int16_t. int16_t my_array[37][73] = {{**DATA HERE**}} I
I have implemented my router framework where I basically use a lookup table to
I would like to create a lookup table in OCaml. The table will have
I have the following lookup table: lkp <- data.frame( x=c(0,0.2,0.65,0.658,1.3,1.76,2.7), y=c(1,1,1,0.942,0.942, 0.92, 0.89) )
I have look-up-table as defined below and I'm making use of GCC. When I
I have many tables that use Lookup/Enum references for most of their column values.
I have some jquery code that is doing an ajax lookup and returning comma

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.