LINQ to SQL Insert Multiple Tables Question?

The pk can be auto generated when the table's definition in the db does it for you. Also the property for the corresponding pk on the linq model has to configured to be updated after the insert, so it gets the auto generated ID I don't think the relation on those tables is on your linq model. Otherwise you should be able to do: using (var context = MatrixDataContext.Create()) { var empPlan = new tblEmploymentQuestionnaire { CommunityJob = communityJob, InsertDate = DateTime.

Now, InsertUser = user, JobDevelopmentServices = jobDevelopmentServices, JobDevelopmentService = new tblEmploymentJobDevelopmetService(), PrevocServices = prevocServices, PrevocService = new tblEmploymentPrevocService(), PrevocServicesID =empPrevocID, TransitionedPrevocToIntegrated =transitionedPrevocIntegrated, EmploymentServiceMatchPref = empServiceMatchPref }; context. TblEmploymentQuestionnaires. InsertOnSubmit(empPlan); context.SubmitChanges(); } ps.

Not having the relation in the model is a design decision, so the above doesn't mean that's the only way to do it. The way you showed (with the extra SubmitChanges calls as in the other answer) is perfectly valid, just responds to a different design.

The pk can be auto generated when the table's definition in the db does it for you. Also the property for the corresponding pk on the linq model has to configured to be updated after the insert, so it gets the auto generated ID. I don't think the relation on those tables is on your linq model.

Otherwise you should be able to do: using (var context = MatrixDataContext.Create()) { var empPlan = new tblEmploymentQuestionnaire { CommunityJob = communityJob, InsertDate = DateTime. Now, InsertUser = user, JobDevelopmentServices = jobDevelopmentServices, JobDevelopmentService = new tblEmploymentJobDevelopmetService(), PrevocServices = prevocServices, PrevocService = new tblEmploymentPrevocService(), PrevocServicesID =empPrevocID, TransitionedPrevocToIntegrated =transitionedPrevocIntegrated, EmploymentServiceMatchPref = empServiceMatchPref }; context. TblEmploymentQuestionnaires.

InsertOnSubmit(empPlan); context.SubmitChanges(); } ps. Not having the relation in the model is a design decision, so the above doesn't mean that's the only way to do it. The way you showed (with the extra SubmitChanges calls as in the other answer) is perfectly valid, just responds to a different design.

I do have this I just didn't know that it was possible. – Refracted Paladin May 13 '10 at 18:49.

I think the issue is (if I understand it correctly) you are deferring the inserting, except you don't know it... Since you're creating FKs but differing their insertion until the end, it doesn't know what to do, so when you try to create the main entry it's enforcing the FK constraints (which might not exist yet), thus failing. Try creating the FK entries and actually submitting the changes to the database before insert the main entry. For example, say you have the following tables: Child Toy ToyOwner ToyOwner has FK constraints on Child and Toy.

If the entries are missing in that table, you will not be able to insert an entry into ToyOwner. So you'd have to do something like the following: Child myChild; Toy myToy; //Queue up the changes that are going to be submitted InsertOnSubmit(myChild) InsertOnSubmit(myToy) //Submit the queue SubmitChanges(); //Now that those FKs are filled, we can insert the main entry with those FK values ToyOwner = new myToyOwner myToyOwner. Child = myChild myToyOwner.

Toy = myToy //And insert the new queue into the DB InsertOnSubmit(myToyOwner) SubmitChanges().

So I'd end up calling SubmitChanges() twice (in my case) to ensure that the PK's get created on those tables so I can use them as FK's on the main table. Sound right? – Refracted Paladin May 13 '10 at 18:03 Out of curiosity, and I will Google this, but how do I Insert an empty row.As in I want the PK to get created but I may not have any other data available at this time.

– Refracted Paladin May 13 '10 at 18:04 You might only have to perform two SubmitChanges() - one that sets up the data used as a FK and one for the main table entry. In terms of creating an empty row - I'd try to avoid that altogether because then you will likely end up with invalid data/states on the database. If anything, try to at least populate it with something, such as a 'not available' FK to another table or something of the sort.

– MunkiPhD May 13 '10 at 19:34.

Also, when inserting I didn't think I needed to provide the PK for a table. It is auto-generated, no? I understand I can use more then 1 InsertOnSubmit(), See this question, I just don't understand how that would apply to my situation and the PK/FK creation.

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