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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:23:57+00:00 2026-05-27T14:23:57+00:00

I have the properties for two entities in a ViewModel. The two entities are

  • 0

I have the properties for two entities in a ViewModel. The two entities are both related to one another, so for example, User and Posts. Each User can have multiple Posts, and Many Posts can belong to a single user (one-to-many).

The aim from my ViewModel is to allow the addition of a User and a Post on the same form. So my ViewModel looks something like this:

public class CreateVM
{
    [Required, MaxLength(50)]
    public string Username { get; set; }

    [Required, MaxLength(500), MinLength(50)]
    public string PostBody { get; set; }

    // etc with some other related properties
}

In my Controller on the Create Method I have something like this:

[HttpPost]
public ActionResult Create(CreateVM vm)
{
    if (ModelState.IsValid)
    {
            User u = new User()
            {
                Username = vm.Username,
                // etc populate properties
            };

            Post p = new Post()
            {
                Body = vm.PostBody,
                // etc populating properties
            };

            p.User = u; // Assigning the new user to the post.

            XContext.Posts.Add(p);

            XContext.SaveChanges();
    }
}

It all looks fine when I walk through it through the Debugger, but when I try to view the post, its User relationship is null!

I also tried

u.Posts.Add(p);

UPDATE:

My Post class code is as follows:

public class Post
{
    [Key]
    public int Id { get; set; }
    [Required, MaxLength(500)]
    public string Body { get; set; }
    public int Likes { get; set; }
    [Required]
    public bool isApproved { get; set; }
    [Required]
    public DateTime CreatedOn { get; set; }
    [Required]
    public User User { get; set; }
}

But that also did not work. What am I doing wrong?

  • 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-27T14:23:57+00:00Added an answer on May 27, 2026 at 2:23 pm

    Problem is that EF can not lazy load the User property because you haven’t made it virtual.

    public class Post
    {
        [Key]
        public int Id { get; set; }
        [Required, MaxLength(500)]
        public string Body { get; set; }
        public int Likes { get; set; }
        [Required]
        public bool isApproved { get; set; }
        [Required]
        public DateTime CreatedOn { get; set; }
        [Required]
        public virtual User User { get; set; }
    }
    

    If you know beforehand that you are going to access the User property of the post you should eager load the User related to the post.

    context.Posts.Include("User").Where(/* condition*/);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities, EntityA and EntityB. EntityB can optionally be related to EntityA,
I have two related one to many entities Race and Cars (on race contains
The two key event argument classes KeyEventArgs and PreviewKeyDownEventArgs each have two properties, KeyCode
I've got two Silverlight Controls in my project, both have properties TeamId. I would
I have a many-to-many relationship between the two entities called events and artists, both
I have two EF entities. One has a property called HouseNumber. The other has
I created two entities and they have navigation properties. var list = from u
I have two properties files that are not the same and I need to
According to MSDN , a hash function must have the following properties: If two
I have a uiviewcontroller with two properties: trackName and playerObject. PlayerObject also has a

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.