当我尝试执行 Update-Database 时,出现此错误:
导航属性“SenderId”不是“对话”类型的声明属性。验证它没有被明确地从模型中排除,并且它是一个有效的导航属性。
编辑
我认为问题在于对话和用户之间的映射关系,因为对话和用户有两个一对多的关系,即对话有两个指向用户的外键
以下是用户和对话的连接方式:
用户:
public class User
{
[Key]
[HiddenInput(DisplayValue = false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserId { get; set; }
public virtual ICollection<Conversation> ConversationSenders { get; set; }
public virtual ICollection<Conversation> ConversationRecievers { get; set; }
对话:
public class Conversation
{
[Key]
[HiddenInput(DisplayValue = false)]
public Guid ConversationId { get; set; }
[ForeignKey("SenderId")]
public Guid SenderId { get; set; }
[ForeignKey("RecieverId")]
public Guid RecieverId { get; set; }
[InverseProperty("ConversationSenders")]
public virtual User Sender { get; set; }
[InverseProperty("ConversationRecievers")]
public virtual User Reciever { get; set; }
}
这是完整的代码:
用户:
public class User
{
[Key]
[HiddenInput(DisplayValue = false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid CollegeId { get; set; }
public int RoleId { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
public string Gender { get; set; }
//role
public DateTime? DateOfBirth { get; set; }
public string ImageURL { get; set; }
[ForeignKey("CollegeId")]
public virtual College College { get; set; }
[ForeignKey("RoleId")]
public virtual UserRole UserRole { get; set; }
public virtual ICollection<Advert> Adverts { get; set; }
public virtual ICollection<Competition> Competitions { get; set; }
public virtual ICollection<Message> Messages { get; set; }
public virtual ICollection<Conversation> ConversationSenders { get; set; }
public virtual ICollection<Conversation> ConversationRecievers { get; set; }
public virtual ICollection<UserOS> UserOses { get; set; }
对话:
public class Conversation
{
[Key]
[HiddenInput(DisplayValue = false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ConversationId { get; set; }
[ForeignKey("SenderId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SenderId { get; set; }
[ForeignKey("RecieverId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid RecieverId { get; set; }
[InverseProperty("ConversationSenders")]
public virtual User Sender { get; set; }
[InverseProperty("ConversationRecievers")]
public virtual User Reciever { get; set; }
public virtual ICollection<Message> Messages { get; set; }
}
信息
public class Message
{
[Key]
[HiddenInput(DisplayValue = false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid MessageId { get; set; }
[HiddenInput(DisplayValue = false)]
public Guid UserId { get; set; }
[HiddenInput(DisplayValue = false)]
public Guid ConversationId { get; set; }
public string Text { get; set; }
public bool? IsSeen { get; set; }
[ForeignKey("UserId")]
public virtual User ConversationSender { get; set; }
[ForeignKey("ConversationId")]
public virtual Conversation Conversation { get; set; }
}