Why is LINQ to SQL entity association creating a new (duplicate) row when inserting a new record?

Since the User entity has been previously loaded by another DataContext (which should hopefully be disposed by now! ), you have to attach it to the new (current) DataContext otherwise the DataContext will view it as a new Entity and not an existing one (which already exists in the DB).

Thank you for your answer. I look forward to the next version of LINQ-to-SQL which will hopefully be a complete ORM with support detaching and reattaching without too much trouble in a more intuitive way than the many posts I found searching for an answer to something that wasn't really a dilemma at all, but instead a misunderstanding on my part of the technology. – mkelley33 Jul 9 '09 at 13:04 This is a less acceptable solution when the thing that you have to attach is passed in from a higher-level URL1 may work once, but when the higher-level cache tries to insert another object, the world explodes because the cached entity could only be attached once, and because it was attached at a lower level of abstraction, it can't know that it needs to be re-detached in the cache.

Linq to sql is such a pain. – Greg D Jul 29 '09 at 20:39.

Make sure you're using the same instance of DataContext for both tables. See my question here for (I think) a clearer explanation of the problem.

I would love to use the same instance of DataContext, but I can't since it's being serialized over WCF. – mkelley33 May 2 '09 at 12:07.

My Old Frustration is Not an Answer, I Apologize: I had marked below as an answer, its unmarked now. "I've decided to answer my own question since it is apparent that I have more of a gripe with LINQ-to-SQL as an incomplete, unintuitive ORM at this stage in its development. Don't get me wrong: it's a great technology, but I was hoping to use it as fully functional ORM without having to write additional plumbing or decorate the existing functionality.

Bottom line: LINQ-to-SQL as an ORM or over WCF is (currently) a "World of Pain". Consider N Subsonic or some other ORM if you need many-to-many functionality or think you might want something more than a bunch of dumb DTOs. H And yes, I've seen the posts and examples how to do many to many in LINQ, but ORMs should be intuitive.

LINQ-to-SQL has a ways to go in that regard. Thank you everyone for your comments and suggestions.

This does not answer the question, please consider choosing one of the answers from others or at least removing this as best answer. I am having the same problem as you were and this choice only makes the question look answered. – brian Jul 8 '09 at 23:41 I'm sorry, you're right.

I haven't checked this in a while, and I was just frustrated at the time. I'll mark it unanswered. Thanks for keeping the overflow honest.

– mkelley33 Jul 9 '09 at 12:58.

It makes sense, that LINQ tries to Insert a New user, because you tell it to do so. If you just want to change one of your users you need to select him out of SomeEntities, do the update and then call SubmitChanges (LINQ will recognize, that the original entity has been modified) - without .InsertOnSubmit.

1 Well, I could have written: someEntity. UserId = this.User. UserId, but that feels strange since I'm trying to associate (an existing user) this.

User with my someEntity instance. "someEntity" could be any entity that I want to associate with an existing User. I just think I'm at odds with LINQ-to-SQL's implementation.It doesn't feel very ORM-ish if the behavior is to insert all children regardless of the implied relationship.

Don't get me wrong, I like LINQ, but this behavior just seems weird. – mkelley33 Apr 24 '09 at 12:23.

I am trying to insert a new entity using LINQ-to-SQL, and entity is associated with a User entity. The insert of the new entity is successful, but my existing User entity gets inserted as if it were a new User. Does anyone know why the user is being inserted as a brand new entity into the Users table?

It would seem that the User. UserId would become the foreign key value in the UserId column of the row mapped to the someEntity that is being inserted.

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