Deal of the Day

Home » Main » Manning Forums » 2007 » LINQ in Action

Thread: Add multiple child entities, but ChangeSet only sees one

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 4 - Pages: 1 - Last Post: Sep 1, 2009 5:08 PM by: sydneyos Threads: [ Previous | Next ]
sydneyos

Posts: 3
Registered: 9/1/09
Add multiple child entities, but ChangeSet only sees one
Posted: Sep 1, 2009 10:37 AM
  Click to reply to this thread Reply

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

Posts: 3
Registered: 9/1/09
Re: Add multiple child entities, but ChangeSet only sees one
Posted: Sep 1, 2009 2:17 PM   in response to: sydneyos in response to: sydneyos
  Click to reply to this thread Reply

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


Posts: 225
From: France
Registered: 4/28/06
Re: Add multiple child entities, but ChangeSet only sees one
Posted: Sep 1, 2009 3:55 PM   in response to: sydneyos in response to: sydneyos
  Click to reply to this thread Reply

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

Posts: 123
From: Atlanta, GA
Registered: 11/9/07
Re: Add multiple child entities, but ChangeSet only sees one
Posted: Sep 1, 2009 4:40 PM   in response to: sydneyos in response to: sydneyos
  Click to reply to this thread Reply

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

Posts: 3
Registered: 9/1/09
Re: Add multiple child entities, but ChangeSet only sees one
Posted: Sep 1, 2009 5:08 PM   in response to: sydneyos in response to: sydneyos
  Click to reply to this thread Reply

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!

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor