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 812185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:09:52+00:00 2026-05-15T01:09:52+00:00

I have a Customer table with a Primary key (int auto increment) and an

  • 0

I have a Customer table with a Primary key (int auto increment) and an Address table with a foreign key to the Customer table. I am trying to insert both rows into the database in one nice transaction.

using (DatabaseDataContext db = new DatabaseDataContext())
{
    Customer newCustomer = new Customer()
    {
        Email = customer.Email
    };

    Address b = new Address()
    {
        CustomerID = newCustomer.CustomerID,
        Address1 = billingAddress.Address1
    };

    db.Customers.InsertOnSubmit(newCustomer);
    db.Addresses.InsertOnSubmit(b);
    db.SubmitChanges();
}

When I run this I was hoping that the Customer and Address table automatically had the correct keys in the database since the context knows this is an auto incremented key and will do two inserts with the right key in both tables.

The only way I can get this to work would be to do SubmitChanges() on the Customer object first then create the address and do SubmitChanges() on that as well. This would create two roundtrips to the database and I would like to see if I can do this in one transaction. Is it possible?

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-15T01:09:52+00:00Added an answer on May 15, 2026 at 1:09 am

    Klaus basically already specified what you need to do – try this code:

    using (DatabaseDataContext db = new DatabaseDataContext())
    {
        Customer newCustomer = new Customer()
        {
            Email = customer.Email
        };
    
        Address b = new Address()
        {
            Address1 = billingAddress.Address1
        };
    
        newCustomer.Address = b;
    
        db.Customers.InsertOnSubmit(newCustomer);
        db.SubmitChanges();
    }
    

    If you associate the address b with the customer you’ve just created, and then insert that customer into the db.Customers collection, calling db.SubmitChanges() should automatically save the address, save the customer and fix up any of the IDENTITY columns to make this work. It does work for me in a test case, for sure.

    You cannot use the address’ or customer’s ID just yet – those haven’t been set yet. But you can definitely associate the full objects with one another and thus get the “connection” between the two in place.

    For this to work, you need to make sure that in the DBML designer, in the Properties window for both ID columns, the Auto Generated Value is True and the Auto-Sync is ste to OnInsert (both of which aren’t the defaults).

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

Sidebar

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.