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

时光总嘲笑我的痴心妄想 提交于 2019-12-10 13:47:02

问题


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

回答1:


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 =)




回答2:


[ForeignKey("SenderId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SenderId { get; set; }

[ForeignKey("RecieverId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid RecieverId { get; set; }

these two line mistakenly marked with [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute.remove these two attribute from properties.corrected version should be :

[ForeignKey("User")]    
public Guid SenderId { get; set; }

[ForeignKey("User")]   
public Guid RecieverId { get; set; }


来源:https://stackoverflow.com/questions/13773334/the-navigation-property-senderid-is-not-a-declared-property-on-type-conversat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!