2017-03-17 47 views
0

我想配置ApplicationUser表和User表之間的'一對一或一個'關係,其中ApplicationUser表是主體,學生是從屬。但是,我希望有一個單獨的Primary keyStudent表和Foreign Key,其中引用ApplicationUser表的ApplicationUserIDMVC 5代碼第一個實體框架可選關係從FK分離PK

public class Student 
{ 
    [Key] 
    [Required] 
    [StringLength(10)] 
    public string StudentCode { get; set; } 

    [Required] 
    [StringLength(15)] 
    [Column(TypeName = "varchar")] 
    public string ContactNum { get; set; } 

    public virtual ApplicationUser User { get; set; } 

    [ForeignKey("User")] 
    public string ApplicationUserID { get; set; } 
} 

public class ApplicationUser : IdentityUser 
{ 
    public virtual Students Students { get; set; } 
} 

我有問題,這個遷移代碼作爲它提出了一個錯誤:

Student_User_Source:多重不是在關係中的作用「Student_User_Source「Student_User」有效。因爲「依賴角色」屬性不是關鍵屬性,所以「依賴角色」的多重性的上界必須爲「*」。

那麼如何配置呢?

回答

0

一對一或零關係做如下因素

Class ApplicationUser 
{ 
[Key] 
Int ApplicationUserId 

// other properties 
} 

Class Student 
{ 
[Key] 
Int StudentId 

Int? AppUserId 

[ForeignKey("AppUserId")] 
Virtual ApplicationUser AppUser 
} 

現在學生可以有一個或零ApplicationUser

另外,你有外鍵的註釋逆轉..它是可能的這是唯一的問題

相關問題