我是ASP.NET MVC的新手,我几个小时都遇到了一个小问题。我有一个简单的类来模拟消息。
这是我的代码:
public class Message
{
[Key]
public int Id { get; set; }
// AspNetUsers
public ApplicationUser Sender { get; set; }
public ApplicationUser Receiver { get; set; }
// SenderId of the message
[Required]
[ForeignKey("Sender")]
[Display(Name = "Sender")]
[InverseProperty("MessageSenderId")]
public string SenderId { get; set; }
// ReceiverId of this message
[Required]
[ForeignKey("Receiver")]
[Display(Name = "Receiver")]
[InverseProperty("MessageReceiverId")]
public string ReceiverId { get; set; }
// other properties
}
SenderId
和ReceiverId
分别是此邮件的发件人和收件人的Id
。当我想将模型传递给视图时,Sender
和Receiver
是导航属性,用于从数据库中获取它们。
不幸的是,当我想添加新迁移时,会出现此错误:
ALTER TABLE语句与FOREIGN KEY约束冲突" FK_dbo.Mails_dbo.AspNetUsers_ReceiverId"。冲突发生在数据库" DbName",table" dbo.AspNetUsers",column' Id'
更新
以前的Message模型是:
public class Message
{
public int Id { get; set; }
// No Navigation is defined
// SenderId of the message
[Required]
[Display(Name = "Sender")]
public string SenderId { get; set; }
// ReceiverId of this message
[Required]
[Display(Name = "Receiver")]
public string ReceiverId { get; set; }
// other properties
}
我做错了什么?感谢