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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:32:28+00:00 2026-06-03T11:32:28+00:00

I’ve been trying to debate on how to add information on a table with

  • 0

I’ve been trying to debate on how to add information on a table with child tables. When I initialize a User, the user needs to have the password encrypted and the data inserted into the child classes. As I am not fully familiar with the Repository, Unit of Work Patterns, I want to ensure I do this properly the first time around. Thanks!

My tables are:

User, UserRoles

My Code:

[HttpPost]
    public ActionResult AddUser(User user)
    {
        try
        {
            if (ModelState.IsValid)
            {
                uow.UserRepository.Insert(user);
                uow.Save();
                return RedirectToAction("Index", "User");
            } 
        }
        catch (DataException)
        {
            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
        }
        return View(user);

    }

public class UserRepository : IUserRepository, IDisposable
{
    private StudentSchedulingEntities _context;

    public UserRepository(StudentSchedulingEntities context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        _context = context;
    }
    public IEnumerable<User> GetUsers()
    {
        return _context.Users.ToList();
    }
    public User GetUserByID(int id)
    {
        return _context.Users.Find(id);

    }
    public void InsertStudent(User user)
    {
        _context.Users.Add(user);
    }
    public void DeleteStudent(int userID)
    {
        User usr = _context.Users.Find(userID);
        _context.Users.Remove(usr);
    }
    public void UpdateStudent(User user)
    {
        _context.Entry(user).State = EntityState.Modified;
    }
    public void Save() {
        _context.SaveChanges();
    }
    public void Dispose()
    {
        Dispose(true); 
        GC.SuppressFinalize(this); 
    }
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_context != null)
            {
                _context.Dispose();
                _context = null;
            }
        }
    }
}

 public interface IUserRepository : IDisposable
{
    IEnumerable<User> GetUsers();
    User GetUserByID(int userID);
    void InsertStudent(User user);
    void DeleteStudent(int userID);
    void UpdateStudent(User user);
    void Save();
}

public class UnitOfWork : IDisposable
{
    private StudentSchedulingEntities _context = new StudentSchedulingEntities();
    private GenericRepository<User> userRepository;

    public GenericRepository<User> UserRepository
    {
        get
        {

            if (this.userRepository == null)
            {
                this.userRepository = new GenericRepository<User>(_context);
            }
            return userRepository;
        }
    }        

    public void Save()
    {
        _context.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Update From Comments:

[HttpPost]
    public ActionResult AddUser(AddUserViewModel auvm)
    {
        try
        {
            if (ModelState.IsValid)
            {
                uow.UserRoleRepository.Insert(auvm.UserRole);
                uow.UserRepository.Insert(auvm.User);

                uow.Save();
                return RedirectToAction("Index", "User");
            } 
        }
        catch (DataException)
        {
            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
        }
        return View(auvm);

    }

public class AddUserViewModel 
{
    public User User { get; set; }
    public UserRole UserRole { get; set; }
    public UserRoleType UserRoleType { get; set; }
    public IEnumerable<SelectListItem> UserRoleTypes { get; set; }      
}
  • 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-06-03T11:32:45+00:00Added an answer on June 3, 2026 at 11:32 am

    Simply create a new User object. Assign the UserRoles to the user. Save the user object. The context class acts as the unit of work – it saves everything only when you call Save Changes.

    1. Create a new user
    2. Add the roles to the user (if required)
    3. Call repository InsertStudent
    4. Call Save
      Your code above seems to be doing this except for the roles, so far it seems OK to me.

    Some choose to go the ViewModel route (which is recommended) so you would be posting a UserCreateViewModel which looks very similar to your User class. This way if the user class changes or you don’t want everything exposed to the user, you can more easily handle these cases.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.