EF Core:无导航属性且基于非主键映射多对多关系
问题描述
我有以下两个实体类:
public class UserQuestion { public Guid UserId { get; set; } public bool HasCompletedQuestion { get; set; } public Guid? QuestionInformationSetId { get; set; } public DateTimeOffset CreatedAt { get; set; } public List<QuestionInformation> QuestionInformation { get; set; } = new(); }
public class QuestionInformation { public Guid Id { get; set; } public string Title { get; set; } public string Text { get; set; } public int Order { get; set; } public Guid SetId { get; set; } public bool Archived { get; set; } }
请问是否可以在一端无导航属性的情况下,基于非主键(QuestionInformationSetId和SetId)映射多对多关系?
我尝试过如下配置:
builder.HasMany(i => i.QuestionInformation) .WithMany(); .UsingEntity<Dictionary<string, object>>( "UserQuestionInformation", j => j.HasOne<QuestionInformationProjection>().WithMany().HasForeignKey("QuestionInformationSetId").HasPrincipalKey("SetId"));
但EF Core迁移会尝试添加连接表来管理UserQuestion与QuestionInformation的关系,我期望无需连接表,即可从UserQuestion导航到QuestionInformation。
解答
你要的这种无需连接表的多对多关系映射在EF Core中无法实现,因为EF Core的多对多关系依赖中间连接表来维护两个实体间的关联关系。但你的场景本质上是一对多关系,完全可以通过一对多的配置来实现无连接表的导航,具体配置如下:
builder.HasMany(uq => uq.QuestionInformation) .WithOne() // QuestionInformation端无导航属性,保持空参数即可 .HasForeignKey(qi => qi.SetId) .HasPrincipalKey(uq => uq.QuestionInformationSetId);
配置说明:
HasMany(uq => uq.QuestionInformation):声明UserQuestion拥有多个QuestionInformation.WithOne():指定QuestionInformation端没有对应的反向导航属性.HasForeignKey(qi => qi.SetId):将QuestionInformation.SetId设为外键.HasPrincipalKey(uq => uq.QuestionInformationSetId):指定UserQuestion端的关联键为非主键的QuestionInformationSetId
这样配置后,EF Core会直接通过SetId和QuestionInformationSetId的匹配关系建立关联,不会生成额外的连接表,你可以正常通过UserQuestion.QuestionInformation导航到对应的集合。
注意事项:
- 确保
QuestionInformationSetId(Guid?)和SetId(Guid)的类型兼容,由于前者可空,该关联是可选的(即UserQuestion可以没有关联的QuestionInformation) - 如果后续需要从
QuestionInformation反向导航到UserQuestion,只需在QuestionInformation类中添加public UserQuestion UserQuestion { get; set; },并将.WithOne()改为.WithOne(qi => qi.UserQuestion)即可
内容的提问来源于stack exchange,提问作者maxime
相关产品推荐
相关产品推荐

