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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:33:34+00:00 2026-05-31T17:33:34+00:00

I am working with an edit user view that includes First name, Last Name,

  • 0

I am working with an edit user view that includes First name, Last Name, Username, etc.

I am also incorporating IsApproved and IsLockedOut from the Membership table in this edit user view as well.

See code snippet:

//Username from User table
<div class="editor-label">
    @Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.UserName)
    @Html.ValidationMessageFor(model => model.UserName)
</div>

//IsApproved from Membership table
<div class="editor-label">
    @Html.LabelFor(model => model.aspnet_Membership.IsApproved)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.aspnet_Membership.IsApproved)
    @Html.ValidationMessageFor(model => model.aspnet_Membership.IsApproved)
</div>

I can go to edit the user, but when I try to save the changes, the following comes up:

"A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship."

and takes place with the following code: db.Users.Attach(user); where user is passed into the method Edit in the controller.

No errors occur when I take away the code associated with the Membership table.

How do I go about resolving this issue? Is there a better approach?

UPDATE: Here’s what’s going on in the controller (in a nutshell):

 public ActionResult Edit(int id)
 {
     User user = db.Users.Single(u => u.UserID == id);

     ViewBag.UserGUID = new SelectList(db.Memberships, "UserId", "Password", user.UserGUID);
     ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "CompanyName", user.CompanyID);

     return View(user);
 }

 public ActionResult Edit(User user)
 {
     if (ModelState.IsValid)
     {
         db.Users.Attach(user);
         db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
         db.SaveChanges();
     }

     ViewBag.UserGUID = new SelectList(db.Memberships, "UserId", "Password", user.UserGUID);
     ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "CompanyName", user.CompanyID);

     return View(user);
 }
  • 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-31T17:33:36+00:00Added an answer on May 31, 2026 at 5:33 pm

    I would create a model with all the properties you need, so you can use it in the view. Then you can separate your data operations between the different tables.

    public ActionResult Edit(int id)
    {
        User user = db.Users.Single(u => u.UserID == id);
        UserViewModel model = new UserViewModel()
        {
            UserID = user.UserID,
            //etc.
        }
    
        ViewBag.UserGUID = new SelectList(db.Memberships, "UserId", "Password", user.UserGUID);
        ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "CompanyName", user.CompanyID);
    
        return View(model);
    } 
    
    public ActionResult Edit(UserViewModel userModel)
    {
        if (ModelState.IsValid)
        {
            // Update user properties
            User user = db.Users.Single(u => u.UserID == userModel.UserID);
            // etc.
    
            // If you need to update the membership value, do that next
    
            db.SaveChanges();
        }
    
        ViewBag.UserGUID = new SelectList(db.Memberships, "UserId", "Password", user.UserGUID);
        ViewBag.CompanyID = new SelectList(db.Companies, "CompanyID", "CompanyName", user.CompanyID);
    
        return View(user);
    }
    

    It’s not as short in the code as doing the Attach(), but it should allow more fine-grained control over your data operations.

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

Sidebar

Related Questions

I am trying to make a view that allows a user to edit a
I'm working on a system were a user can edit existing objects (Filter domain
I'm working on an app for the ipad 2 that lets the user record
Working on an ASP.NET MVC 3 (Razor) application, that's mainly concerned with UGC (user
I'm working on a strongly-typed edit form for a MVC model that contains a
I'm working in an iPad app that has a split view with a navigation
I'm working on an app that is in landscape view. I'm using a few
I'm working on a simple note-taking/painting app. I have a view controller that works
I won't want to have edit any working sets. I just want a way
im using the WPFToolkit's DataGrid and im trying to get an edit button working,

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.