-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem deleting items from child collections #183
Comments
@bobgodfrey Thanks for posting the issue, and I'm glad you like TE! It would be easier if you could create a GitHub repo for this and paste the link here. Then I can clone and run it to see what's happening. Cheers! |
Wow you were fast to comment but the good news is that I see the error of my ways, sorry user error. I was not using the object returned from the collection tracker in the apply changes. See code changes with the //IMPORTANT comment and the use of the trackedOrder object. Sorry I have a few questions and don’t know the best forum to ask? My use case is to have an object graph stored in local or external session that is fully disconnected, basically a serialized object that is a temporary working copy. The object will get multiple updates over multiple post backs and eventually should be committed at the end of a long running process. This is really all server side changes. Is this some that I can/should do with trackable entities? I am not sure how to handle the change tracker. I would assume that I would need to maintain a reference to the change tracker as well during the post backs? I also noticed that the change tracker is cloning the objects during the GetChanges() and if I can get away with it would rather not have the extra work to clone the objects. If I was to design around deleting objects and moved to a soft delete were the status of the object “deleted” was persisted to the DB that would be ok with me. Given my first example everything worked with the exception of the deletes, that would work for me. I also prototyped the web version with local session and multiple post backs and everything else worked on my simple parent/child test with the exception of deletes. I don’t know if you had this in mind with the design of trackable entities? The other question is how do you use the server side only objects, the ones that use theses interfaces - ITrackable, IMergeable, (vs the shared TE/EF objects that derive from EntityBase) to track and save changes if all the changes are done in server side in process? I looked in the samples and it was not clear. I will see if I can set up a repo later today if you still want to see the original example
var trackedOrder = changeTracker.GetChanges().FirstOrDefault();
|
Here is the repo with the original problem if you still need it. |
Tony,
I really like the framework and just getting started so I am assuming that I am doing something incorrect. I have an issue removing items from child collections and getting them deleted the database. It seems to be isolated to the ApplyChanges method as it is not attaching the deleted items to the DB context??? In the example below, I am inserting a new order with details, loading the order (and details) from the DB, and then updating the loaded order/details, finally saving the order. The issue is that I am removing the second order detail item from the order.OrderDetails collection but it does not get deleted. It looks like the change tracking is working correctly as the trackable entities state is correct but again it never attaches the deleted child item to the DB context to set the EF state so the record still exists in the database after the save. It may be that you didn’t intend for this use case as I am interested in server side only tracking of disconnected entities.
See the example below used with the northwindslim DB. I am running the latest and greatest from Nuget for all and specifically the TrackableEntities.EF.6 project - 2.5.4. I can send you my sample project if it’s easier?
Also, if I was to debug this what branch are your nugget release processed on?
Thanks
Bob
The output looks like this –
Insert new Order
Loaded the new Order from DB with no tracking
After db.ApplyChanges the second order detail state is Detached and should be Deleted
First order detail - EF state: Modified Trackable Entity State: Modified
Second order detail - EF state: Detached Trackable Entity State: Deleted
third order detail - EF state: Unchanged Trackable Entity State: Unchanged
fouth order detail - EF state: Added Trackable Entity State: Added
Press any key to terminate
The code is below.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using TrackableEntities.Client;
using TrackableEntities.Common;
using TrackableEntities.EF6;
namespace TE_DeleteIssue
{
class Program
{
static void Main(string[] args)
{
Test1_SaveParentChild Test1_SaveParentChild = new Test1_SaveParentChild();
Test1_SaveParentChild.Test();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press any key to terminate");
Console.ReadLine();
}
}
public class Test1_SaveParentChild
{
}
The text was updated successfully, but these errors were encountered: