-->

MVC3从另一个表获得一个编号时,我有Membership.GetUser()。ProviderUs

2019-09-17 05:53发布

我是比较新的MVC的世界,正在开发一个MVC3,C#.NET,使用默认的Microsoft成员资格提供EF4应用。 有需求有附加到成员资格提供表格,这有助于弥补了每个用户的扩展配置一些附加字段。 (保存该数据的表被称为资料,它具有简档INT的PK和还持有aspnet_Users / aspnet_Membership的用户ID的FK)。

我想提供新用户的机会,注册后立即填写自己的个人资料,所以我将其重定向到一个页面,在这里他们可以输入我们想要的配置表马上值。 报名工作正常。 用户创建,如在数据库中的个人资料记录。

当事情出错时,我得到了用户到配置文件编辑页面,我试图拉起自己的数据。 我进入System.Web.Security.Membership.GetUser()。ProviderUserKey以获取用户标识,然后根据要找到自己的档案记录。

这没有工作,我想我已经想通了,但我一定是什么地方搞错了,现在不能让它正常工作。 该错误信息是非常通用的,似乎表明,你找不到使用用户ID的GUID值的简档INT值。

    // 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);
    }

现在的错误信息:

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);

如何是db.Profiles.Find(currentUser)可以专门针对发现即使默认/假定ID列简档INT用户ID GUID值的任何想法?

Answer 1:

搜索查找基于它的主键,而你的情况是一个int,并没有相关的成员资格提供密钥(这是一个GUID)的表。 所以你不能使用查找。

相反,使用LINQ查询,像这样的:

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


文章来源: MVC3 getting an Id from another table when I have the Membership.GetUser().ProviderUserKey