The navigation property 'SenderId' is not a declared property on type 'Conversation'

12,039

Finally I've found solution, stupid mistake. In Conservation it should be

    [ForeignKey("Sender"), Column(Order = 0)]

    public Guid SenderId { get; set; }

    [ForeignKey("Receiver"), Column(Order = 1)]

    public Guid ReceiverId { get; set; }

And not

[ForeignKey("SenderId"), Column(Order = 0)]
[ForeignKey("ReceiverId"), Column(Order = 1)]

After that I got error:

"Introducing FOREIGN KEY constraint 'FK_dbo.Conversations_dbo.Users_ReceiverId' on table 'Conversations' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors."

And solution is: in DbContext this code:

        modelBuilder.Entity<Conversation>()
           .HasRequired(s => s.Sender)
           .WithMany(s => s.ConversationSenders)
           .HasForeignKey(s => s.SenderId)
           .WillCascadeOnDelete(false);


        modelBuilder.Entity<Conversation>()
            .HasRequired(r => r.Receiver)
            .WithMany(r => r.ConversationReceivers)
            .HasForeignKey(r => r.ReceiverId)
            .WillCascadeOnDelete(false);

I've tested it and now everything works fine =)

Share:
12,039
hyperN
Author by

hyperN

I have been programming since I was 10 and can't stop :)

Updated on June 04, 2022

Comments

  • hyperN
    hyperN almost 2 years

    When I try to do Update-Database, I get this error:

    The navigation property 'SenderId' is not a declared property on type 'Conversation'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

    Edit

    I believe problem is in mapping relations between Conversation and User, because Conversation and User are connected with two one to many relationships i.e. Conversation has two foreign keys pointing to User

    Here is how User and Conversation are connected:

    User:

    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; }
    

    Conversation:

    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; }
    
    }
    

    Here is whole code:

    User:

    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; }
    

    Conversation:

    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; }
    }
    

    Message

     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; }
    }