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

  • Home
  • SEARCH
  • 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 6793711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:08:00+00:00 2026-05-26T18:08:00+00:00

I made a while ago some code that adds an object to my table

  • 0

I made a while ago some code that adds an object to my table with entity framework code first.

Context.Reactions.Attach(reaction);
Context.SaveChanges();

After that I used a relationship called Profile to get the name of the user that posted the reaction.

reaction.Profile.FirstName + " " + reaction.Profile.LastName

Now after a while, I get a nullreference on Profile when I try to get the name of the person.

So why can’t I add an object and after that use the Profile relationship?

Edit:
I’m getting the nullreference on the Profile relation property. So reaction.Profile says that is null. It is the same instance. Entity Framework has given me an id for the reaction so the savechanges() works.
The line for getting the FirstName is the line directly after the SaveChanges so the context couldn’t be disposed right?

Complete code:

var reaction = new Reaction()
                                {
                                    Text = reactionText,
                                    ProfileId = CurrentProfileId,
                                    PostedOn = DateTime.Now
                                };

                    //Save changes to the backend
                    context.Reactions.Add(reaction);
                    context.SaveChanges();

                    return Json(new
                    {
                        Id = reaction.Id,
                        ReactionType = reactionType,
                        ReactionTypeId = reactionTypeId,
                        ReactionText = reaction.Text,
                        PostedOn = reaction.PostedOn.ToString("G"),
                        ProfileName = string.Format("{0} {1}", reaction.Profile.FirstName, reaction.Profile.LastName)
                    });

I even tryed with this code around it.

using (var context = new SeeTingsContext())
                {
                    context.Configuration.LazyLoadingEnabled = true;


                }
  • 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-26T18:08:00+00:00Added an answer on May 26, 2026 at 6:08 pm

    I didn’t quite get you when I read after a while. Does this mean you get the relation object first, and then it suddenly becomes null?
    My guess is:

    • You have not set LazyLoadingEnabled to true for the context you’re working on.
    • Your context is disposed and thus you cannot use it to get the relationship. Note that you can use Lazy loading while your context still exists.

    Update: Based on the comments, try to load the relation explicitly and see if you can get the profile:

    if (!context.Entry(reaction).Reference(r => r.Profile).IsLoaded)
    {
        context.Entry(reaction).Reference(r => r.Profile).Load();
    }  
    

    If you cannot get profile using this way, I suppose there’s something wrong with your mapping.

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

Sidebar

Related Questions

Hi I made this site a while ago in my table days but have
A while ago I used a python framework that could control the mouse and
I made a script a while ago that wrote to a file, I did
So I have a validation script I made a while ago. I can pass
This is something that made me doubt for a while so I thought it
While discussing a Java synchronization question , someone made a comment that the following
I made a windows service. My code is similar to: onstart() { while(true) {
I made a repository as a test a while ago but I now want
A while ago, I wrote a web-based guestbook application that wrote it's own database.
I made the transition from C++ to objective-C a while ago, and am now

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.