I want to configure a 'one to zero or one' relationship between ApplicationUser
table and User
table whereby ApplicationUser
table is the principal and Student is the dependent. However, I would like to have a seperate Primary key
for Student
table and a Foreign Key
that references ApplicationUser
table's ApplicationUserID
.
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; }
}
I've problem migrating this code as it raises an error of:
Student_User_Source: : Multiplicity is not valid in Role 'Student_User_Source' in relationship 'Student_User'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
So how do I configure this?
For one to one or zero relation do the folowing
Class ApplicationUser
{
[Key]
Int ApplicationUserId
// other properties
}
Class Student
{
[Key]
Int StudentId
Int? AppUserId
[ForeignKey("AppUserId")]
Virtual ApplicationUser AppUser
}
Now a Student can have one or Zero ApplicationUser
Plus you had the annotation of the foreign key reversed .. it is possible that this was the only problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.