1

我遇到了导航属性未加载的问题。我的所有其他实体都有相同的设置,但这是使用不是自然 FK(数字)并且不会级联的属性,它将由触发器处理。

Expression<Func<DivisionBracketGameParticipant, object>>[] includes2 = {
                                                                                       q => q.DivisionWinnerBracketGame,
                                                                                       q => q.DivisionLoserBracketGame
                                                                                   };
            var test = _divisionBracketGameParticipantsRepository.GetMany(includes2,
                                                                      q =>
                                                                      q.DivisionBracketGame.DivisionBracket.Division.
                                                                          EventId == eventId);

数据库模式

DivisionBracketGame

Id
Number

DivisionBracketGameParticipant

Id
DivisionBracketGameId -> Id
DivisionBracketGameWinnerNumber -> Number
DivisionBracketGameLoserNumber -> Number

实体

[Table("DivisionBracketGame", Schema = "GrassrootsHoops")]
    public class DivisionBracketGame : BaseEntity
    {
        public int Id{ get; set; }
        public int Number { get; set; }

        [InverseProperty("DivisionBracketGame")]
        public virtual ICollection<DivisionBracketGameParticipant> DivisionBracketGameParticipants { get; set; }

        [InverseProperty("DivisionWinnerBracketGame")]
        public virtual ICollection<DivisionBracketGameParticipant> DivisionWinnerBracketGameParticipants { get; set; }

        [InverseProperty("DivisionLoserBracketGame")]
        public virtual ICollection<DivisionBracketGameParticipant> DivisionLoserBracketGameParticipants { get; set; }
    }

[Table("DivisionBracketGameParticipant", Schema = "GrassrootsHoops")]
public class DivisionBracketGameParticipant : BaseEntity
    {
        public int Id{ get; set; }
        public virtual int DivisionBracketGameId { get; set; }
        public virtual int? DivisionWinnerBracketGameNumber { get; set; }
        public virtual int? DivisionLoserBracketGameNumber { get; set; }

        [ForeignKey("DivisionBracketGameId")]
        public virtual DivisionBracketGame DivisionBracketGame { get; set; }

        [ForeignKey("DivisionWinnerBracketGameNumber")]
        public virtual DivisionBracketGame DivisionWinnerBracketGame { get; set; }

        [ForeignKey("DivisionLoserBracketGameNumber")]
        public virtual DivisionBracketGame DivisionLoserBracketGame { get; set; }
    }
4

1 回答 1

0

EF 不会创建关系,Number因为它不是主键。主键是你DivisionBracketGameId所以两者DivisionWinnerBracketGame都是DivisionLoserBracketGame目标Id(不是Number)。

一对多关系要求主表中的列是唯一的 - 在您的情况下,该列应该是Number. 这可以通过使用主表中的主键或在该列上使用唯一索引在数据库中实现。EF 不支持唯一索引/候选键,因此在 EF 中建立一对多关系的唯一方法是通过主表的主键。

FK 值用于获取相关值,因此目前它可能会查找具有错误值的记录,因为它使用了错误的列。

于 2012-07-10T08:48:41.650 回答