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

  • Home
  • SEARCH
  • 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 3693864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:25:40+00:00 2026-05-19T04:25:40+00:00

To represent our course tree structure in our Linq Dataclasses we have 2 columns

  • 0

To represent our course tree structure in our Linq Dataclasses we have 2 columns that could potentially be the same as the PK.

My problem is that if I want to Insert a new record and populate 2 other columns with the PK that was generated there is no way I can get the next identity and stop conflict with other administrators who might be doing the same insert at the same time.

Case: A Leaf node has right_id and left_id = itself (prereq_id)

**dbo.pre_req:**
prereq_id
left_id
right_id
op_id
course_id
is_head
is_coreq
is_enforced
parent_course_id

and I basically want to do this:

        pre_req rec = new pre_req
        {
            left_id = prereq_id,
            right_id = prereq_id,
            op_id = 3,
            course_id = query.course_id,
            is_head = true,
            is_coreq = false,
            parent_course_id = curCourse.course_id
        };

        db.courses.InsertOnSubmit(rec);

        try
        {
            db.SubmitChanges();
        }

Any way to solve my dilemma? Thanks!

  • 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. Editorial Team
    Editorial Team
    2026-05-19T04:25:40+00:00Added an answer on May 19, 2026 at 4:25 am

    You don’t generate the Identity yourself, or directly set the ID values for FKs.

    So, simplifying things down (because your structure isn’t terribly clear)

    If your Person table looks like:

       Id  int IDENTITY(1,1)
       Name nvarchar(MAX)
       MotherId int NULL  (FK -> Person.Id)
       FatherId int NULL  (FK -> Person.Id)
    

    Then your person Class in LINQ should look like:

    public class Person 
    {
       public int Id { get; set;}
       public string Name { get; set;} 
       public Person Mother { get; set;}
       public int? MotherId { get; set;}
       public Person Father { get; set;}
       public int? FatherId { get; set;}
    }
    

    When inserting, you can do this:

    var mother = new Person { Name = "April Smith" }; 
    var father = new Person { Name = "Bob Smith" }; 
    var child = new Person { Name = "Charlie Smith", Mother = mother, Father = father}; 
    
    db.People.InsertOnSubmit(mother);
    db.People.InsertOnSubmit(father);
    db.People.InsertOnSubmit(child); 
    

    LINQ-to-SQL should then know to grab the generated Ids for mother and father, and insert them into the child record.

    If you’re trying to self-referential or circular link, then there’s no way to do this in a single transaction that I know of.

    You would have to insert your records first (and leave their references null initially) and then come back and update the references later.

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

Sidebar

Related Questions

We're using interfaces to represent entity classes in our domain model. We have concrete
I need to represent inheritance like Person/Student. How do you do that in LInqToSql?
We have a highly specialized DAL which sits over our DB. Our apps need
We have several different domains hosted on an in-house server. They all represent different
Most of time we represent concepts which can never be less than 0. For
How do i represent CRLF using Hex in C#?
How to represent the start and end times for one day? Using October 23,
How can I represent the following in XSD. <price-update> <![CDATA[ arbitrary data goes here
or how can i represent an address to country mapping where an address has
How would you represent a temporal many-to-many relation in SQL? Under non-temporal circumstances one

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.