我有2个彼此需要FK的实体:
public class MessagesGroup
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long GroupId { get; set; }
public string Title { get; set; }
public DateTime Created { get; set; }
public int? LastMessageId { get; set; }
[Required]
[ForeignKey("GroupId,LastMessageId")]
public virtual Messages Message { get; set; }
}
public class Messages
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long GroupId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int MessagesId { get; set; }
public int UserId { get; set; }
[StringLength(2000)]
public string Text { get; set; }
public DateTime Date { get; set; }
[StringLength(255)]
public string AttachmentUrl { get; set; }
[Required]
[ForeignKey("GroupId")]
public virtual MessagesGroup MessageGroup { get; set; }
[Required]
[ForeignKey("GroupId,UserId")]
public virtual MessagesGroupUsers MessagesGroupUsers { get; set; }
}
如何在两个实体上获取FK,因为现在我遇到了错误
无法确定类型“ MDC.Repository.Model.Messages”和“ MDC.Repository.Model.MessagesGroup”之间的关联的主要终点。必须使用流利的API或数据注释来显式配置此关联的主要端。`我尝试了其他解决方案,没有任何帮助,只出现了新的错误。
编辑
我尝试过该解决方案
public class MessagesGroup
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long GroupId { get; set; }
public int? LastMessageId { get; set; }
public virtual Messages Message { get; set; }
}
public class Messages
{
[Key]
[Column(Order = 0)]
[ForeignKey("MessageGroup")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long GroupId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int MessagesId { get; set; }
public int UserId { get; set; }
[Required]
public virtual MessagesGroup MessageGroup { get; set; }
[Required]
[ForeignKey("GroupId,UserId")]
public virtual MessagesGroupUsers MessagesGroupUsers { get; set; }
}
现在我得到
多重性在关系“ Messages_MessageGroup”中的角色“ Messages_MessageGroup_Source”中无效。由于从属角色属性不是关键属性,因此从属角色多重性的上限必须为'*'。
通过查看此新消息,我感到他无法映射msgGroupId,但我不知道为什么。