views:

863

answers:

4

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. The code looks something like the following:

var someEntity = new Entity();
someEntity.User = this.User;
dataContextInstance.SomeEntities.InsertOnSubmit(someEntity);
dataContextInstance.SubmitChanges();

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.

Thanks for any help/suggestions/comments

A: 

Sorry, I don't get it? Do you want to update your existing user? What is someEntity for?

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.

timo2oo8
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
A: 

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.

Shaul
I would love to use the same instance of DataContext, but I can't since it's being serialized over WCF.
mkelley33
+4  A: 

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).

RobS
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
This is a less acceptable solution when the thing that you have to attach is passed in from a higher-level cache. It 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
A: 

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 NHibernate, 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."

mkelley33
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
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