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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:48:44+00:00 2026-05-13T17:48:44+00:00

I have a User table and a ClubMember table in my database. There is

  • 0

I have a User table and a ClubMember table in my database. There is a one-to-one mapping between users and club members, so every time I insert a ClubMember, I need to insert a User first. This is implemented with a foreign key on ClubMember (UserId REFERENCES User (Id)).

Over in my ASP.NET MVC app, I’m using LinqToSql and the Repository Pattern to handle my persistence logic. The way I currently have this implemented, my User and ClubMember transactions are handled by separate repository classes, each of which uses its own DataContext instance.

This works fine if there are no database errors, but I’m concerned that I’ll be left with orphaned User records if any ClubMember insertions fail.

To solve this, I’m considering switching to a single DataContext, which I could load up with both inserts then call DataContext.SubmitChanges() only once. The problem with this, however, is that the Id for User is not assigned until the User is inserted into the database, and I can’t insert a ClubMember until I know the UserId.

Questions:

  1. Is it possible to insert the User into the database, obtain the Id, then insert the ClubMember, all as a single transaction (which can be rolled back if anything goes wrong with any part of the transaction)? If yes, how?

  2. If not, is my only recourse to manually delete any orphaned User records that get created? Or is there a better way?

  • 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-13T17:48:44+00:00Added an answer on May 13, 2026 at 5:48 pm

    You can use System.Transactions.TransactionScope to perform this all in an atomic transaction, but if you are using different DataContext instances, it will result in a distributed transaction, which is probably not what you really want.

    By the sounds of it, you’re not really implementing the repository pattern correctly. A repository should not create its own DataContext (or connection object, or anything else) – these dependencies should be passed in via a constructor or public property. If you do this, you’ll have no problem sharing the DataContext:

    public class UserRepository
    {
        private MyDataContext context;
    
        public UserRepository(MyDataContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            this.context = context;
        }
    
        public void Save(User user) { ... }
    }
    

    Use the same pattern for ClubMemberRepository (or whatever you call it), and this becomes trivial:

    using (MyDataContext context = new MyDataContext())
    {
        UserRepository userRep = new UserRepository(context);
        userRep.Save(user);
        ClubMemberRepository memberRep = new ClubMemberRepository(context);
        memberRep.Save(member);
        context.SubmitChanges();
    }
    

    Of course, even this is a little bit iffy. If you have a foreign key in your database, then you shouldn’t even need two repositories, because Linq to SQL manages the relationship. The code to create should simply look like this:

    using (MyDataContext context = new MyDataContext())
    {
        User user = new User();
        user.Name = "Bob";
        user.ClubMember = new ClubMember();
        user.ClubMember.Club = "Studio 54";
    
        UserRepository userRep = new UserRepository(context);
        userRep.Save(user);
        context.SubmitChanges();
    }
    

    Don’t fiddle with multiple repositories – let Linq to SQL handle the relationship for you, that’s what ORMs are for.

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

Sidebar

Related Questions

I have a user table 'users' that has fields like: id first_name last_name ...
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a USER table in database. The table has a RegistrationDate column which
I have a user table in my database which contains two columns FirstName and
I have a User table, which is having Userid(primary key), Username, Password There is
I have a User table that has serialized data in one of the tables.
I have a User table where there are a Username and Application columns. Username
I have a User entity which represents a User table in my database. This
I have a user table in my mysql database that has a password column.
We have a user table, every user has an unique email and username. We

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.