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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:47+00:00 2026-05-27T05:51:47+00:00

This one is driving me absolutely insane, especially because I have a suspicion that

  • 0

This one is driving me absolutely insane, especially because I have a suspicion that either there is an easy fix or I’m asking too much of the EF…

The situation is this:

I have a user control (ASCX) that is basically serving as an edit form for an EF entity. When the control databinds I pull the object from the database by ID and place it in the control state (by overriding SaveControlState() and LoadControlState()).

The user then goes about their merry way making any changes or what-not. This object has navigation properties on it so when they make a change to a navigation property, say by adding a location to collection of locations on the object, I am updating the dataItem in control state.

Finally, after the user is all done and clicks the save button I try to save or create the record using the following code:

protected void SaveButton_Click(object sender, EventArgs e)
{
    DepartmentLookup dept = Master.DataContext
        .Departments.Find(ResourceDepartment.SelectedValue.ToInt());

    LocationLookup location = dataItem.Locations[ResourceLocation.SelectedIndex];
    if (dataItem.OfficeLocation == null)
    {
        dataItem.OfficeLocation = new OfficeLocationLookup()
        {
            Location = location,
            OfficeLocationName = location.LocationName
        };
    }
    else if (!dataItem.OfficeLocation.Location.Equals(location))
    {
        dataItem.OfficeLocation.Location = location;
        dataItem.OfficeLocation.OfficeLocationName = location.LocationName;
    }

    foreach (LocationLookup loc in dataItem.Locations)
    {
        if (loc.LocationTypeID == default(int))
        {
            LocationTypeLookup locType = Master.DataContext
                .LocationTypes.SingleOrDefault(lt =>
                    lt.LocationType == loc.LocationType.LocationType);

            if (locType != null)
                loc.LocationType = locType;
        }
        else
        {
            LocationTypeLookup locType = Master.DataContext
                .LocationTypes.Find(loc.LocationTypeID);
            if (locType.LocationType != loc.LocationType.LocationType)
            {
                LocationTypeLookup newType = new LocationTypeLookup()
                {
                    LocationType = loc.LocationType.LocationType
                };

                loc.LocationType = newType;
            }
        }
    }

    dataItem.PrimaryPhone = PrimaryPhone.Text;
    dataItem.CellPhone = CellPhone.Text;
    dataItem.Department = dept;
    dataItem.EmailAddress = EmailAddress.Text;
    dataItem.LastModifiedBy = HttpContext.Current.User.Identity.Name;
    dataItem.LastModifiedDtm = DateTime.UtcNow;

    if (dataItem.ResourceID == default(int))
        Master.DataContext.Resources.Add(dataItem);
    else
    {
        DbEntityEntry<Resource> entry = Master.DataContext.Entry<Resource>(dataItem);
        if (entry != null && entry.State == EntityState.Detached)
        {
            Master.DataContext.Resources.Attach(dataItem);
            // entry.State = EntityState.Modified;
        }
    }

    Master.DataContext.SaveChanges();
}

I’ve tried a number of different ways to try and save the object to the database, all resulting in various errors. The only method that didn’t throw an exception was the SetValues method, which also didn’t save any of the navigation properties and thus proved to be of no value.

Any help or suggestions at all would be greatly appreciated here as I’ve been racking my brain on this problem for a couple days now.

Thanks in advance!
J

  • 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-27T05:51:47+00:00Added an answer on May 27, 2026 at 5:51 am

    I had a similiar problem when saving entity objects in the session (rather than control state). The problem was that retrieved objects has a variety of different ObjectContexts and this caused the saves to fail.

    It depends on how you are managing the context but the best idea is to associate the context with the Request object and use a factory class to retrieve it therefore you are using one context per request.

    HttpContext.Current.Items["Context"] = context
    

    Any other pattern caused me big problems.

    In my experience it’s not a good idea to save the entire entity in the Session (or ControlState) because of the difficulty in managing the contexts. The contexts will have come from different requests even if the above (very safe) pattern is used. I ended up just saving the object ids into Session(ControlState) and using a wrapper class to retrieve them. I persisted the objects that I wanted to pass around in the Request collection rather than anything that persists past postbacks.

    This is my very plaintive question with my own struggles in a similar area. Hope it’s of some use.

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

Sidebar

Related Questions

OK, this one is driving me nuts.... I have a string that is formed
This one has been driving me nuts. I have a div that has it's
Ok, this one is driving me nuts. I have a backend file uploader that
This one's driving me crazy... I have a loop which adds an event listener
I'm tearing my hair out over this one - its driving me mad because
This is driving me crazy. I have this one php file on a test
This is a dumb newbie question, but one that is driving me nuts. I
This one is driving me crazy:-) Lets say I have some test xaml code:
I know jQuery pretty well. This one is driving me nuts because it's SO
this is driving me absolutely crazy. I'm trying to deploy an application to one

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.