In an n-tier application linq-to-sql doesn't seem to have a clear cut solution for updating a disconnected entity that has child EntitySets.
I have some linq-to-sql entities...
public partial class Location : INotifyPropertyChanging, INotifyPropertyChanged
{
public int id;
public System.Nullable<int> idLocation;
public string brandingName;
public System.Data.Linq.Binary timeStamp;
public EntitySet<LocationZipCode> LocationZipCodes;
}
public partial class LocationZipCode : INotifyPropertyChanging, INotifyPropertyChanged
{
public string zipcode;
public string state;
public int idLocationDetail;
public int id;
public System.Data.Linq.Binary timeStamp;
public EntityRef<Location> Location;
}
So a Location
entity would have an EntitySet
of LocationZipCodes
.
The Location
domain model gets mapped to a view model which the presentation layer consumes and then ultimately sends back the changed view model entity where it's mapped back to the Location
domain model. From there I update the entity and save the changes. Here's the handler:
public class ProgramZipCodeManagerHandler : IHttpHandler {
private LocationsZipCodeUnitOfWork _locationsZipCodeUnitOfWork = new LocationsZipCodeUnitOfWork();
public void ProcessRequest(HttpContext context) {
if (context.Request.HttpMethod == "POST") {
string json = Json.getFromInputStream(context.Request.InputStream);
if (!string.IsNullOrEmpty(json)) {
Location newLocation = Json.deserialize<Location>(json);
if (newLocation != null) {
//this maps the location view model from the client to the location domain model
var newDomainLocation = new Mapper<Location, DomainLocation>(new DomainLocationMapTemplate()).map(newLocation);
if (newDomainLocation.id == 0)
_locationsZipCodeUnitOfWork.locationRepository.insert(newDomainLocation);
else
_locationsZipCodeUnitOfWork.locationRepository.update(newDomainLocation);
_locationsZipCodeUnitOfWork.saveChanges(ConflictMode.ContinueOnConflict);
var viewModel = new Mapper<DomainLocation, Location>(new LocationMapTemplate()).map(newDomainLocation);
context.Response.ContentType = "application/json";
context.Response.Write(Json.serialize(viewModel);
}
}
}
}
}
Here is the update method within my locationRepository
:
protected System.Data.Linq.Table<T> _table;
public void update(T entity) {
_table.Attach(entity, true);
_context.Refresh(RefreshMode.KeepCurrentValues, entity);
}
public void update(T newEntity, T oldEntity) {
_table.Attach(newEntity, oldEntity);
_context.Refresh(RefreshMode.KeepCurrentValues, newEntity);
}
I can see that all records directly associated with the Location
entity are being updated but the child collection (public EntitySet<LocationZipCode> LocationZipCodes
) is not being updated.
Is there a clear cut way of updating a disconnected entity that has a child EntitySet that also needs to be updated? In other words, I have a detached entity that has a collection of another entity. That collection has changed and I need to update that in the database.
No.. you cannot do that.
Attaching and Detaching objects is per the object you working with and will not affect related objects (entities).
You can read further information from here:
Attaching and Detaching Objects
Consider a situation where object A is related to a collection B which is filled with 1000 values. You detach A and send it to some remote processing - A is sent with null in the B relation.. and now A is brought back to your program - there is no way to know that A.B null is a result of the remote processing or was it given to the remote processing already with null.
In the link posted with this answer - please scroll down to read carefully at the section titled:
Considerations for Detaching Objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With