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
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.
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.