I am using Code First approach while creating Database using Entity Framework Core. I would like to create two foreign keys pointing the same table. My example shows User table which will hold userID and Message table which will hold both Receiver ID and Sender ID (what means both values have to point the same table).
Code for User:
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string UserName { get; set; }
[Required]
[MaxLength(50)]
public string Password { get; set; }
public virtual ICollection<Message> MessagesSent { get; set; }
public virtual ICollection<Message> MessagesReceived { get; set; }
}
For Message:
public class Message
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public User Sender { get; set; }
public User Receiver { get; set; }
public int senderId { get; set; }
public int receiverId { get; set; }
[MaxLength(500)]
public string message { get; set; }
}
I am using ASP.NET Core 2 and I am a newbie. I was trying to use this solution, but unfortunately, I couldn't manage to override OnModelCreating method. It shows that it doesn't exist.
PS. don't mind password field, it is only for testing purpose.
Thank you!