3 Replies - 11470 Views - Last Post: 24 January 2012 - 06:14 PM

#1 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

MVC 3 LINQ to Entities ... db update controller action method problem

Posted 24 January 2012 - 09:31 AM

Hello everyone...

So in my MVC 3 project I decided to use the built-in Account/Membership system and so I integrated those necessary tables in with the rest of the tables... The problem is with an Update Controller Action Method, when I go to save changes to a profile, I get this error:

"The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Profiles_aspnet_Users". The conflict occurred in database "***", table "dbo.aspnet_Users", column 'UserId'. The statement has been terminated."

The thing is, it shouldn't be trying to update the aspnet_Users table, it should be updating the Profiles table... Anyone have any ideas?

Here's a snapshot of the aspnet_Users table, Profiles table, and the Foreign Key...

Posted Image
Posted Image
Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: MVC 3 LINQ to Entities ... db update controller action method problem

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: MVC 3 LINQ to Entities ... db update controller action method problem

Posted 24 January 2012 - 03:57 PM

You should post the Controller code. That will shed some light on the problem, you might be setting a property on an entity you don't mean to, or even not setting the foreign key at all.
Was This Post Helpful? 0
  • +
  • -

#3 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: MVC 3 LINQ to Entities ... db update controller action method problem

Posted 24 January 2012 - 04:47 PM

View PostSergio Tapia, on 24 January 2012 - 04:57 PM, said:

You should post the Controller code. That will shed some light on the problem, you might be setting a property on an entity you don't mean to, or even not setting the foreign key at all.


Here is the controller action method for the update:

        [HttpPost]
        public ActionResult Edit(Profile profile)
        {
            if (ModelState.IsValid)
            {
                db.Entry(profile).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName", profile.UserId);
            return View(profile);
        }



But seriously, I'm about to scrap this project the way it is... I tried it by integrating and using the built in Account/Membership system, but it's really messing up more than I feel it's worth...

I wanted to use my own Membership system, but there just are simply no good tutorials on the topic that will walk a person through how to customize one... I mean all I wanted was a user to be able to register and be able to log in and for that I used the aspnet_regsql.exe tool to integrate the Account/Membership tables in with mine... I then deleted my Member table, changed my Profile table to use the aspnet_Users ==> UserId as the Foreign Key... and wow, what I mess it created...

I know this is off topic now, but I have just got to figure out how to use my own tables instead of theirs, but somehow be able to use their [Authorize] decoration... I heard rumors that MVC 5 is going to have a really nice customization integration system for the Membership, but that's over a year away...

For the 11 tables that their Membership system installs, all I need is this...

Members
MemberID int, identity, primary key
MemberName nvarchar(25)
MemberPassword nvarchar (25)
MemberEmail nvarchar (75)
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: MVC 3 LINQ to Entities ... db update controller action method problem

Posted 24 January 2012 - 06:14 PM

Creating your own custom membership isn't hard at all. Would you be interested in a tutorial of sorts? I had to build this for a freelance project I'm working on right now, and it basically boils down to creating your own User, Role, UserRoles table, implementing your own AuthorizeAttribute and overriding two functions.

As for your code, don't work directly with Entity objects. Roll your own Models and work with them, shape them twirk them bop 'em, then place them into Entity objects. So you would do something like:

[HttpPost]
public ActionResult Edit(ProfileModel profileModel)
{
    if (ModelState.IsValid)
    {
        var profileEntity = repository<Profile>.GetById(profileModel.ProfileId); // POCO PROPERTY.
        profileEntity.Name = profileModel.Name; // Set the name for change!
        repository<Profile>.SaveChanges(); // This has to call the dbEntities.SaveChanges() method.
        return RedirectToAction("Index");
    }
    ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName", profile.UserId);
    return View(profileEntity); // Again, look how we're using a ViewModel here. :)/>
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1