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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:06:45+00:00 2026-06-02T22:06:45+00:00

I have the following method inside my repository class, to retrieve a Session object

  • 0

I have the following method inside my repository class, to retrieve a Session object and the associated Medicine object (by defining Eager loading using the .Include) as shown below:-

public Session GetSession(int id)
        {
            return entities.Sessions.Include(d => d.Medicine).FirstOrDefault(d => d.SessionID == id);

        }

My action method which call the above repository method look as follow:-

[HttpPost]
        public ActionResult Delete(int id)
        {

            try
            {
                //string desc;
                var s = repository.GetSession(id);

                repository.DeleteSession(s);

                repository.Save();
                return Json(new { IsSuccess = "True", id = s.SessionID, description = s.Medicine.Name }, JsonRequestBehavior.AllowGet);
            }
            catch (ArgumentNullException)
//code goes here

The problem i am facing is that after physically deleting the object using repository.Save(); ,, i will not be able to access the Medicine navigation property from the memory and the following exception will be raised NullReferenceException was unhandled by user code on description = s.Medicine.Name , while i can access the s.SessionID which will be avilable in the memory even after deleting the object, so does this means that the Session object which was deleted did not Include (did not eager load) the Medicine navigation property !!!?
BR

  • 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-02T22:06:46+00:00Added an answer on June 2, 2026 at 10:06 pm

    If you delete an entity which has a relationship to another entity, Entity Framework will remove the relationship between those objects at the same time. You can solve the problem by creating the object for the Json result before you delete the entity:

    var s = repository.GetSession(id);
    
    var result = new { IsSuccess = "True", id = s.SessionID,
                       description = s.Medicine.Name };
    
    repository.DeleteSession(s);
    repository.Save();
    
    return Json(result, JsonRequestBehavior.AllowGet);
    

    If there is no reference from the session to Medicine in the database this won’t help of course because Include won’t return a related object. You have to handle this case separately (it would be only possible if the relationship is not required).

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

Sidebar

Related Questions

I have the following static method inside a static class. My question is it
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have the following method I came across in a code review. Inside the
i have the following model method inside my asp.net MVc web application:- public IQueryable<User>
I have the following method signature: public static void InvokeInFuture(Delegate method, params object[] args)
I have the following domain object: public class DomainObject<T,TRepo> where T : DomainObject<T> where
I have following method having following line inside it HtmlPage.Window.Invoke(showPopUp,new string[] {Hello from Silverlight});
I have the following code inside a method invoked by a jsf <h:commandButton>. It

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.