Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:42:47+00:00 2026-05-11T01:42:47+00:00

I’m trying to figure out the best way to save a simple one-to-many relationship

  • 0

I’m trying to figure out the best way to save a simple one-to-many relationship in Linq2Sql.

Lets assume we have the following POCO model (pseduo code btw):

Person has zero to many Vechicles.

class Person {     IList<Vehicle> Vehicle; }  class Vehicle {     string Name;     string Colour; } 

Now, when i save a Person, i pass that poco object to the repository code (which happens to be L2S). I can save the person object fine. I usually do this.

using (Db db = new Db()) {     var newPerson = db.People.SingleOrDefault(p => p.Id == person.Id) ?? new SqlContext.Person();     // Left to right stuff.     newPerson.Name = person.Name;     newPerson.Age = person.Age;      if (newPerson.Id <= 0)         db.People.InsertOnSubmit(newPerson);     db.SubmitChanges(); } 

i’m not sure where and how i should handle the list of vehicles the person might have? any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:42:47+00:00Added an answer on May 11, 2026 at 1:42 am
    using (Db db = new Db()) {     var newPerson = db.People.SingleOrDefault(p => p.Id == person.Id) ?? new SqlContext.Person();     // Left to right stuff.     newPerson.Name = person.Name;     newPerson.Age = person.Age;      // add vehicles.     Vehicle firstV = new Vehicle();     firstV.Name = 'some name';     firstV.Person = newPerson; // need to do this to set the person Id on the vehicle.     newPerson.Vehicle.Add(firstV);     // now when you save the Person it should save the Vehicle list      // if you set Cascade save update on the list. (not sure how to do that in L2S      if (newPerson.Id <= 0)         db.People.InsertOnSubmit(newPerson);     db.SubmitChanges(); } 

    Now you may choose to construct the list of vehicles at another level , with the data that’s coming from the interface.

    But you need to remember that it’s not enough to add the Vehicle to the list on the Person object , you also need to set the vehicles Person property to the person that has the vehicles.

    Observation I’m not sure about this but when you do db.People.SingleOrDefault you might be loading the whole People table in memory . That’s not something you want to do. Corrected by Slace in the comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If you're using Google Maps v2, call checkResize() on your… May 11, 2026 at 1:02 pm
  • added an answer From MSDN: Version 4.71 and later of Shell32.dll adds the… May 11, 2026 at 1:02 pm
  • added an answer Both events will be marshalled onto the UI thread. May 11, 2026 at 1:02 pm

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I have a web-service that I will be deploying to dev, staging and production.
I'm thinking of starting a wiki, probably on a low cost LAMP hosting account.
I have the following tables in my database that have a many-to-many relationship, which
I'm using the RESTful authentication Rails plugin for an app I'm developing. I'm having
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.