The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

sydneyos (3) [Avatar] Offline
#1
Here is my situation.
1. Retrieve an Entity from the db
2. Add more than one child entities in a given EntitySet
3. When I go to save, GetChangeSet only finds the first child as an entity to be inserted

I've tried this with multiple parent-child pair types and have the same outcome. I've tried adding child entities one at a time and using AddRange and get the same result.

I am completely stumped. Anyone else seen this?
sydneyos (3) [Avatar] Offline
#2
Re: Add multiple child entities, but ChangeSet only sees one
Here is my test case to replicate this behavior:

int ct;
using (SurveyDB db = new SurveyDB())
{
var branch = db.GetTable<Branch>().SingleOrDefault(t => t.SurveyId == 10);
ct = branch.Steps.Count + 2;
List<Step> children = new List<Step>();
children.Add(new Step { StepKey = "Manual1", Description = "Manual 1", StepType = 8 });
children.Add(new Step { StepKey = "Manual2", Description = "Manual 2", StepType = 8 });
branch.Steps.AddRange(children);
ChangeSet set = db.GetChangeSet();
db.SubmitChanges();
}
using (SurveyDB db = new SurveyDB())
{
var saved = db.GetTable<Branch>().SingleOrDefault(t => t.SurveyId == 10);
Assert.AreEqual(ct, saved.Steps.Count);
}
fabrice.marguerie (224) [Avatar] Offline
#3
Re: Add multiple child entities, but ChangeSet only sees one
Sydney,

You're making a simple mistake.
Instead of Add and AddRange, you should try with InsertOnSubmit.
e.g.:
branch.Steps.InsertOnSubmit(new Step { StepKey = "Manual1", Description = "Manual 1", StepType = 8 });

See section 6.6 in the book for the details. There is also DeleteOnSubmit.

Fabrice
jwooley (123) [Avatar] Offline
#4
Re: Add multiple child entities, but ChangeSet only sees one
Have you tried directly adding your children to the branch rather than using AddRange? (InsertOnSubmit only applies to the Table<> not to the child EntitySet.) In this case you could do something like:

branch.Steps.Add(new Step { StepKey = "Manual1", Description = "Manual 1", StepType = 8 });
branch.Steps.Add(new Step { StepKey = "Manual2", Description = "Manual 2", StepType = 8 });

It may be helpful to pull out Sql Profiler to see the actual statements issued to the database (you should see 6: Initial fetch, initial fetch of step count, save of step1, save of step2, refetch the saved branch, fetch the saved step count). Looking at that trace may give a hint into the underlying cause of the problem.

Jim Wooley
sydneyos (3) [Avatar] Offline
#5
Re: Add multiple child entities, but ChangeSet only sees one
Found it. This was due to a stupid override of GetHashCode based on the primary key (which is 0 for all new objects, so all new child objects were evaluated as the same). Doh!