2016-06-08 26 views
1

我正嘗試使用Asp.Net MVC框架與Microsoft提供的開箱即用身份系統。如何在.Net Identity ApplicationUser模型中添加一列以指向外國模型?

但是,我需要捕獲更多關於用戶的數據。

爲了最大限度地減少對Identity類的更改,我需要創建一個名爲「User」的新模型,並在其中放入所有自定義屬性。然後我添加了一個名爲「SystemId」的屬性,它是ApplicationUser模型的外鍵。

這裏是我做了什麼

這裏是我的自定義User模型

[Required] 
    [MaxLength(128)] 
    [ForeignKey("Id")] 
    public string SystemId { get; set; } 


    [MaxLength(50)] 
    public string FirstName { get; set; } 

    [MaxLength(50)] 
    public string MiddleName { get; set; } 

    [MaxLength(50)] 
    public string LastName { get; set; } 

    [MaxLength(250)] 
    public string Email { get; set; } 

    [MaxLength(50)] 
    [Index("IX_Username", 1, IsUnique = true)] 
    public string Username { get; set; } 

    [ForeignKey("Role")] 
    public int RoleId { get; set; } 


    [ForeignKey("Client")] 
    public int CurrentClientId { get; set; } 

    [ForeignKey("Campaign")] 
    public int? CurrentCampaignId { get; set; } 

    public string Status { get; set; } 

    public virtual Role Role { get; set; } 

    public virtual Client Client { get; set; } 

    public virtual Campaign Campaign { get; set; } 

    public virtual ApplicationUser ApplicationUser { get; set; } 

    public virtual List<UserToPrivilege> UserToPrivileges { get; set; } 

    public User() 
    { 
     DateTime UtcNow = DateTime.UtcNow; 

     Status = "active"; 

     MiddleName = null; 

     UserToPrivileges = new List<UserToPrivilege>(); 

     CreatedAt = UtcNow; 

     LastUpdatedAt = UtcNow; 
    } 

} 

然後在IdentityModel類我改變了我的ApplicationUser級以下

public class ApplicationUser : IdentityUser 
{ 
    [ForeignKey("User")] 
    public int UserId { get; set; } 

    public virtual User User { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

但是當我嘗試創建像我這樣的遷移

Add-Migration InitialCreate -Force 

我收到以下錯誤

屬性「ID」不能被配置爲導航屬性。 屬性必須是有效的實體類型,並且該屬性應具有非抽象的getter和setter 。對於集合屬性,類型 必須實現ICollection,其中T是有效的實體類型。

我在做什麼錯?如何將自定義User模型鏈接到屬於關係的ApplicationUser模型?

+0

你爲什麼不在User類中繼承ApplicationUser? – prospector

+0

這聽起來像個好主意。當我這樣做,那麼我會有一個或兩個模型?從數據視圖中,我將有一個表而不是兩個? –

+0

應該只有一個,但我不記得我在幾年內沒有這樣做。 – prospector

回答

0

首先改變班級名稱。然後嘗試將public virtual ApplicationUser添加到您的新班級中,希望這可以奏效,因爲爲我工作。 我用新的類UserProfile它看起來像這樣:

[Table("UserProfiles")] 
public class UserProfile 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid Id { get; set; } 
    public virtual ApplicationUser User { get; set; } 
    public string UserId { get; set; } 
    public DateTime CreatedAt { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int Gender { get; set; } 

    public string UrlSeo { get; set; } 
} 

工作正常,我。

+1

我應該改變什麼類名? –

+0

你正在使用類名User,所以如果Identity系統生成相同的名字,所以試着改變它,它會工作,我也與這個問題一起工作,將類名更改爲UserProfile或其他,現在它正在工作。 –

相關問題