2013-03-11 80 views
0

我有表UserProfile,在那里我有一些附加字段,如电子邮件,typeAccount,isActivated等
当我Membership.GetUser().(..),我没有这些字段。
如何解决它?以及如何改变这个领域的价值?附加字段MVC

[Table("UserProfile")] 
public class UserProfile 
{ 

    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 
    public string Email { get; set; } 
    public int typeAccount { get; set; } 
    public bool activated { get; set; } 
    public string activationcode { get; set; } 
    public bool del { get; set; } 
} 

问候

+0

您是否添加了成员​​资格提供程序表? – 2013-03-11 12:30:23

+0

我使用本教程 - http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ – whoah 2013-03-11 12:31:53

+0

你应该接受Darin的回答! – ssilas777 2013-03-11 13:19:57

回答

3

的成员资格提供不处理配置文件。如果要检索当前已通过身份验证的用户的UserProfile记录,只需使用上下文:

[Authorize] 
public ActionResult SomeAction() 
{ 
    using (UsersContext db = new UsersContext()) 
    { 
     UserProfile user = db 
      .UserProfiles 
      .FirstOrDefault(u => u.UserName == User.Identity.Name); 

     // do something with the profile 
    } 

    ... 
} 
+0

Works perfecty!现在我返回所有UserProfile,谢谢! :)你能告诉我,例如如何改变一些UserName的这种方法中的typeAccount? – whoah 2013-03-11 12:48:04

+0

您首先检索UserProfile(使用我在答案中显示的方法),然后修改typeAccount属性的值(例如'user.typeAccount = 123;'),最后在数据上下文中调用SaveChanges方法('db.SaveChanges();')。 – 2013-03-11 12:55:07

+0

非常感谢! :) – whoah 2013-03-11 13:27:40