gpt4 book ai didi

.net - 导航属性 'SenderId' 不是类型 'Conversation' 上的声明属性

转载 作者:行者123 更新时间:2023-12-04 00:26:15 26 4
gpt4 key购买 nike

当我尝试执行 Update-Database 时,出现此错误:

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.



编辑

我认为问题出在 Conversation 和 User 之间的映射关系上,因为 Conversation 和 User 有两个一对多的关系,即 Conversation 有两个外键指向 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; }

对话:
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; }
}

最佳答案

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

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

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

关于.net - 导航属性 'SenderId' 不是类型 'Conversation' 上的声明属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13773334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com