5

当我尝试执行 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; }
}
4

2 回答 2

5

最后我找到了解决方案,愚蠢的错误。在保护中应该是

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

    public Guid SenderId { get; set; }

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

    public Guid ReceiverId { get; set; }

并不是

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

之后我收到错误:

“在表 'Conversations' 上引入 FOREIGN KEY 约束 'FK_dbo.Conversations_dbo.Users_ReceiverId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束。参见前面错误。”

解决方案是:在 DbContext 中这段代码:

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

我已经测试过了,现在一切正常=)

于 2012-12-08T13:52:40.190 回答
0
[ForeignKey("SenderId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SenderId { get; set; }

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

这两行错误地标有 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]属性。从属性中删除这两个属性。更正的版本应该是:

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

[ForeignKey("User")]   
public Guid RecieverId { get; set; }
于 2012-12-08T05:25:35.003 回答