2013-03-11 79 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