包含继承实体的导航属性

本文关键字:导航 属性 实体 继承 包含 | 更新日期: 2023-09-27 18:10:01

我正在使用实体框架,急于加载,我试图加载我的图形的一部分。我有候选人和培训师继承的Person表,当有人同时是培训师和候选人时,我有一个问题(注意:培训师和候选人没有任何额外的属性,只有从Person继承的主键)。

这是我的查询:

RequestInfo ri = context.RequestInfo.Include(x => x.Request).Include(x => x.TeamName).Include(x => x.Sender)
                .Include(x => x.TrainingLanguage).Include(x => x.TrainingLocation).Include(x => x.CcPerson.Select(p => p.Person))
                .Include(x => x.Request.Select(c => c.Candidate))
                .Include(x => x.Request.Select(c => c.Candidate).Select(ct => ct.CandidateType))
                .Include(x => x.Request.Select(c => c.Candidate.CandidateType.Select(cwp => cwp.CandidateWorkPresence)))
                .Where(x => x.Id == r.Id).Single();

这可能会更好,但我有一个问题是,当有人是候选人和培训师时,我得到以下例外:

All objects in the EntitySet 'EmployeesTrainingEntities.Person' must have unique primary keys. However, an instance of type 'EmployeesTrainingModel.Candidate' and an instance of type 'EmployeesTrainingModel.Trainer' both have the same primary key value, 'EntitySet=Person;PersonId=5'.

我试图找到解决这个问题的方法,但是我运气不好,而且我看到很多人都有这个问题。也许我遗漏了什么,所以如果有人有什么解决方案、建议……它是受欢迎的。如果有必要,我会添加一张照片,其中包含我正在加载的模型的一部分。

编辑:

模型包含RequestInfo实体,该实体具有关于训练请求的公共信息。RequestInfoRequestsSender(即Person)和CcPersons(将接受培训的人员)的集合。Request包含Candidate

解决方案:我找到了解决问题的办法。当我有一个人是TrainerCandidate时,我所做的是继承重叠,而似乎EntityFramework不允许继承重叠。所以,我用关联代替了继承关系,现在它可以正常工作了。

包含继承实体的导航属性

如果没有任何附加属性,那么创建两个继承自person的额外实体是没有意义的。

为什么不直接编辑Person表,为PersonRole添加一个enum [flag]属性呢?这样,您就可以为一个用户分配多个角色,并将它们存储在int中。