簡體   English   中英

如何在Asp.Net MVC 4 Simple Membership的用戶配置文件中添加FistName和LastName的其他字段?

[英]How to Add Extra Fields of FistName and LastName in Userprofile of Asp.Net MVC 4 Simple Membership?

我想在UserProfile Asp.Net簡單成員身份中添加2個額外字段,但無法添加,也無法從Internet找到任何幫助。 請給我一些解決方案。

提前致謝

要將字段添加到SimpleMembership中的UserProfile中(我假設您要使用遷移和EF):

  1. 確保已啟用遷移(使用程序包管理器控制台中的Enable-Migration
  2. 編輯UserProfile類(假設您使用的是Visual Studio 2012->新建項目-> ASP.NET MVC 4 Web應用程序-> Internet應用程序模板,將為您創建此模板)以添加新屬性(示例下面)
  3. 添加一個遷移,該遷移將使用Add-Migration命令更新您的數據庫。
  4. (可選)編輯生成的遷移以處理默認值和不可為空的字段。
  5. 使用Update-Database命令對您的開發數據庫運行該遷移。

我已經在此答案中使用UserProfile提供了有關SimpleMembership如何工作的概述。 請注意,可以將UserProfile與所有其他類一起從UsersContext移動到相同的DbContext,而不必在單獨的上下文中。

SimpleMembership旨在與代碼優先EF開發一起很好地使用,所以這就是我上面概述的過程。 一個例子更新UserProfile類與ForenameSurnameLoginCount場會是什么樣子:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }

    [MaxLength(40)]
    [Required]
    public string Forename { get; set; }

    [MaxLength(40)]
    [Required]
    public string Surname { get; set; }

    [Required]
    public int LoginCount { get; set; }
}

參考文獻:

  1. ASP.NET MVC 4實體框架腳手架和遷移 -任務3顯示了如何啟用遷移
  2. MSDN:代碼優先遷移 -您需要了解的有關遷移的所有信息
  3. StackOverflow:如何將自己的數據庫與SimpleMembership和WebSecurity一起使用? 什么是MVC4安全性?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM