-->

MVC3 getting an Id from another table when I have

2019-07-22 01:16发布

问题:

I'm relatively new to the MVC world, and am developing an MVC3, C# .Net, EF4 application that uses the default Microsoft membership provider. There are requirements to have some additional fields appended to the membership provider tables, which help make up an extended profile for each user. (The table that holds this data is called Profile, and it has a PK of ProfileId INT and also holds an FK of the UserId from aspnet_Users/aspnet_Membership.)

I want to offer new users the chance to immediately fill out their profile after registration, and so I'm redirecting them to a page where they can enter the values we want for the Profile table right away. Registration works fine. The user is created, as is the Profile record in the database.

Where things go wrong is when I get the user to that Profile editing page, and I try to pull up their data. I'm going into System.Web.Security.Membership.GetUser().ProviderUserKey to get the UserId, and then finding their Profile record based on that.

This did work, and I thought I had it figured out, but I must have made a mistake somewhere and now cannot get it to work correctly. The error message is fairly generic and seems to indicate that you can't find the ProfileId INT value by using the UserId's GUID value.

    // GET: /Profile/Entry/

    public ActionResult Entry()
    {
        Guid currentUser = (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
        Profile profile = db.Profiles.Find(currentUser);
        ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId", "UserName", profile.UserId);
        return View(profile);
    }

Now the error message:

Server Error in '/' Application.
--------------------------------------------------------------------------------

The argument types 'Edm.Int32' and 'Edm.Guid' are incompatible for this operation. Near     WHERE predicate, line 1, column 76. 
Description: An unhandled exception occurred during the execution of the current web     request. Please review the stack trace for more information about the error and where it     originated in the code. 

Exception Details: System.Data.EntitySqlException: The argument types 'Edm.Int32' and     'Edm.Guid' are incompatible for this operation. Near WHERE predicate, line 1, column 76.

Source Error: 
Line 127:        {
Line 128:            Guid currentUser = (    Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
Line 129:            Profile profile = db.Profiles.Find(currentUser);
Line 130:            ViewBag.UserId = new SelectList(db.aspnet_Users, "UserId",     "UserName", profile.UserId);
Line 131:            return View(profile);

Any ideas on how that db.Profiles.Find(currentUser) can be specifically targeted to find the UserId GUID value even though the default/assumed Id column is ProfileId INT?

回答1:

Find searches a table based on its primary key, which in your case is an int and is not related to the Membership Provider Key (which is a guid). So you cannot use Find.

Instead, use a Linq query, such as this:

Profile profile = db.Profiles.Where(x => x.AspMembershipUserID == currentUser).FirstOrDefault();