public class Message
{
[Key]
public int MeesageId { get; set; }
public int SenderId { get; set; }
[ForeignKey("PersonId")]
public virtual Person Sender { get; set; }
public int ReceiverId { get; set; }
[ForeignKey("PersonId")]
public virtual Person Receiver { get; set; }
public string Content { get; set; }
public DateTime CreatedOn { get; set; }
public bool Seen { get; set; }
}
public class Person
{
public string Username { get; set; }
[Key]
public int PersonId { get; set; }
}
I’m getting this error:
The ForeignKeyAttribute on property ‘Receiver’ on type ‘Finder.Models.Message’ is not valid. The foreign key name ‘PersonId’ was not found on the dependent type ‘Finder.Models.Message’. The Name value should be a comma-separated list of foreign key property names.
What I think I should do is rename ReceiverId
to PersonId
, so it matches the foreign key, but then the property names would be too messy. Any help would be appreciated
2
Answers
The ForeignKey attribute specifies which
int
property is the foreign key for the specified navigation property. SoIt is better to use fluent api.
for example :