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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:26:35+00:00 2026-05-24T05:26:35+00:00

I am new to LINQ to SQL and the repository pattern and was attempting

  • 0

I am new to LINQ to SQL and the repository pattern and was attempting to do a simple example to get an understanding of how exactly my application should be structured. On my User model object I have a method called create(). When a user is created I want to insert into my User table and insert into my user_role table to assign the user the lowest possible role. I have a method on User model called create which does all this work. Shown below. Am I on the right path with how the repository pattern with LINQ to SQL should work.

    public Boolean Create()
    {            
        UserDbDataContext context = new UserDbDataContext();

        // Create user.
        IUserRepository userRepo = new UserRepository(context);            
        userRepo.Create(this);

        // Get role that user should be added to.
        IRoleRepository roleRepo = new RoleRepository(context);
        var role = roleRepo.GetByRoleName("rookie");

        // Insert user into role.
        IUserRoleRepository userRoleRepo = new UserRoleRepository(context);
        User_Role userRole = new User_Role();
        userRole.RoleId = role.Id;
        userRole.UserId = this.Id;
        userRoleRepo.Create(userRole);

        context.Dispose();

        return true;
    }

This is my controller method.

    public ActionResult Register(string username, string password, string email)
    {
        User user = new User();
        user.Username = username;
        user.Password = password;
        user.Email = email;
        user.DateCreated = DateTime.Now;
        user.Create();

        return RedirectToAction("Test");
    }

Is there a better approach I could take to improve the process? Any help would be vastly appreciated.

  • 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-24T05:26:36+00:00Added an answer on May 24, 2026 at 5:26 am

    One of the main problems in your current approach is that the repositories individually have the responsibility to create/update the entities they hold on the database – This means that if you have changes in one repository and update another repository (i.e. your Create() call) both will be committed to the DB.

    The usual way to structure this is having a unit of work (i.e. see here) that sits on top of all repositories that is responsible to commit changes to the DB, this structure also allows for transactions spanning multiple repositories.

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

Sidebar

Related Questions

We're using LINQ to SQL with the repository pattern and DTOs. My question is:
Question: I'm new to the repository pattern, and I'm attempting to implement a new
I am new to LINQ to SQL, but have done a lot of database
im really new to linq-to-SQL so this may sound like a really dumb question,
I am quite new with Linq to SQL and MVC but I follow the
I'm familiar with .NET and with SQL. Now I'm looking at the new LINQ
As far as I can understand, when I new up a Linq to SQL
I'm new at LINQ, searching the net for LINQ samples that mimic SQL's LIKE
I'm trying to create a generic LINQ-TO-SQL repository based on this post which basically
I encapsulate my linq to sql calls in a repository class which is instantiated

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.