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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:09:55+00:00 2026-05-29T09:09:55+00:00

I have a project with classes in a project that I am building in

  • 0

I have a project with classes in a project that I am building in a Code First style. The problem is simple. The objects in question are a User and a userType.

public class User
{
    [Key]
    public int id { get; set; }

    [DisplayName("User Name")]
    [Required(ErrorMessage = "User Name is required.")]
    public string userName { get; set; }

    [DataType(DataType.Password)]
    [DisplayName("Password")]
    [Required(ErrorMessage = "Password is required.")]
    public string userPassword { get; set; }

    [DisplayName("First Name")]
    public string first_Name { get; set; }

    [DisplayName("Last Name")]
    public string last_Name { get; set; }

    [DisplayName("Email")]
    public string email { get; set; }

    [DisplayName("Zip")]
    public string Zip { get; set; }

    [DisplayName("Address")]
    public Address address { get; set; }

    [DisplayName("Birthdate")]
    public DateTime dateOfBirth { get; set; }

    [DisplayName("Home phone")]
    public string homePhone { get; set; }

    [DisplayName("Cell phone")]
    public string cellPhone { get; set; }

    [DisplayName("Remember me")]
    public bool persistCookie { get; set; }

    public UserType userType { get; set; }

    public int status { get; set; }

}

AND

public class UserType
{
    [Key]
    public int id { get; set; }
    public string typeName { get; set; }

}

I generate a User like this:

userType = (from ret in db.UserTypes where ret.typeName.Equals("Contractor") select ret).FirstOrDefault();
                user.userType = userType;
                user.status = 1;
                db.Users.Add(user);
                db.SaveChanges();

The database schema seems to support the foreign key to the UserTypes table.

Everything works well on the add and in the database I see a user record with the appropriate userType_id.

The problem comes when I retrieve the user later on. My User.userType is always null. This has me really confused. If the userType key is in the Users table for the record, why do I not get a userType object as part of my User when I get that user using a standard LINQ query like:

User user = (from ret in db.Users where ret.userName.Equals(userIn.userName) && ret.userPassword.Equals(userIn.userPassword) select ret).FirstOrDefault();

I get the User but user.userType is always null.

What am I missing here?
Should I be building a different LINQ query?
Shouldn’t the userType object be automatically included in the User object?

I think the Code First programming style is an excellent methodology but this problem has me baffled.

Thanks for any help!

  • 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-29T09:09:56+00:00Added an answer on May 29, 2026 at 9:09 am

    You need to tell EF to eager load the navigational property userType by using Include method.

    User user = (from ret in db.Users.Include(u => u.userType)
                 where ret.userName == userIn.userName 
                 && ret.userPassword == userIn.userPassword select ret)
                 .FirstOrDefault();
    

    Or make the userType property virtual so that EF will lazy load it.

    public virtual  UserType userType { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been assigned a project to develop a set of classes that act
This is for a web project so i have several classes that inherit from
In my project I have a class that is inherited by many other classes.
I have a few classes in a project that I inherited that are really
In my project I have put all my css classes in the style sheets.
I have a Maven project that's building fine, and I'm attempting to add a
In one of my projects, I have some classes that represent entities that cannot
In a lot of my PHP projects, I end up with classes that have
I have a project with a number of different classes querying and modifying data
I have a project which it's domain contain following classes: Courier Customer Food Order

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.