导航属性“SenderId”不是“对话”类型的声明属性

时间:2012-12-08 00:56:30

标签: .net entity-framework ef-code-first database-migration ef-migrations

当我尝试更新数据库时,我收到此错误:

  

导航属性“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; }
}

2 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:0)

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

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

这两行错误地标记了[DatabaseGenerated(DatabaseGeneratedOption.Identity)]属性。从properties.corrected版本中删除这两个属性应该是:

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

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