How to save child relationship entities in the Entity Framework?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

You are using multiple ObjectContexts concurrently (the variable context and whereever this came from). Don't do that. It will only make things very difficult for you.

Use one ObjectContext at a time I can give more specific advice if you show more code.

You are using multiple ObjectContexts concurrently (the variable context and whereever this came from). Don't do that. It will only make things very difficult for you.

Use one ObjectContext at a time. I can give more specific advice if you show more code.

The context of "this" is automatically created by the WCF generated code. Is there any way to deal with that? Thanks.

– Sako73 Feb 18 '10 at 20:44 It would be best to use that context instead of making a new one. Again, hard to say without seeing the code. – Craig Stuntz Feb 18 '10 at 21:16.

I suspect you are getting the errors because the ObjectContext thinks you are trying to add a new entity but finds it already has a EntityKey. I use the AttachTo method of the ObjectContext to attach my already existing entities to their EntitySet. I have had results generating my entities from stubs or hitting the database.

This way when you add the entity to the navigation property on your entity, the ObjectContext finds the entity in it's EntitySet and knows it is an existing entity and not a new one. I don't know if this is clear. I could post some code if it would help.As Mr Stuntz said in his answer, posting more of your code would help.

Wow. After 20 or so straight hours on this problem, I'm starting to hate the Entity Framework. Here is the code that appears to be working currently.

I would appreciate any advice on how to make this more streamlined. I did rework the WCF service so that there is only the one data context. Thanks Craig.

Then I had to change the code to the following: MyRole x = context.Roles. FirstOrDefault(a => a. RoleId == this.

RoleId); if (x == null) // inserting { MyApplication t = this. Application; this. Application = null; context.

Attach(t); this. Application = t; } else // updating { context. ApplyPropertyChanges("Roles", this); x.Permissions.Load(); IEnumerable oldPerms = x.Permissions.

Select(y => y. PermissionId); List newPerms = this.Permissions. Where(y =>!oldPerms.

Contains(y. PermissionId)).ToList(); IEnumerable curPerms = this.Permissions. Select(y => y.

PermissionId); List deletedPerms = x.Permissions. Where(y =>!curPerms. Contains(y.

PermissionId)).ToList(); // new foreach (MyPermission p in newPerms) { x.Permissions. Add(context.Permissions. First(z => z.

PermissionId == p. PermissionId)); } // deleted foreach (MyPermission p in deletedPerms) { x.Permissions. Remove(context.Permissions.

First(z => z. PermissionId == p. PermissionId)); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions