.NET 5中如何正确配置EF Core多组同指向Delta实体的一对多映射
我正在使用AutoMapper将来自Camille库的类映射到自定义领域类,计划在我的.NET 5 REST API中使用EF Core存储这些领域类。但有一个特别棘手的类给我带来了大量麻烦。如下所示,ParticipantTimeline类包含多个存储统计数据的字典:
namespace MingweiSamuel.Camille.MatchV4 { public class ParticipantTimeline { [JsonPropertyName("participantId")] public int ParticipantId { get; set; } [JsonPropertyName("csDiffPerMinDeltas")] public IDictionary<string, double> CsDiffPerMinDeltas { get; set; } [JsonPropertyName("damageTakenPerMinDeltas")] public IDictionary<string, double> DamageTakenPerMinDeltas { get; set; } [JsonPropertyName("role")] public string Role { get; set; } [JsonPropertyName("damageTakenDiffPerMinDeltas")] public IDictionary<string, double> DamageTakenDiffPerMinDeltas { get; set; } [JsonPropertyName("xpPerMinDeltas")] public IDictionary<string, double> XpPerMinDeltas { get; set; } [JsonPropertyName("xpDiffPerMinDeltas")] public IDictionary<string, double> XpDiffPerMinDeltas { get; set; } [JsonPropertyName("lane")] public string Lane { get; set; } [JsonPropertyName("creepsPerMinDeltas")] public IDictionary<string, double> CreepsPerMinDeltas { get; set; } [JsonPropertyName("goldPerMinDeltas")] public IDictionary<string, double> GoldPerMinDeltas { get; set; } [JsonExtensionData] public Dictionary<string, object> _AdditionalProperties { get; set; } = new Dictionary<string, object>(); // 省略冗余方法 } }
此前我按照如下方式映射到我的领域类:
- CamilleProfile.cs 映射配置
// 省略其他冗余配置 CreateMap<ParticipantTimeline, ParticipantTimelineDto>(); CreateMap<KeyValuePair<string, double>, ParticipantTimelineDelta>() .ForMember(x => x.Delta, conf => conf.MapFrom(kvp => kvp.Key)) .ForMember(x => x.Value, conf => conf.MapFrom(kvp => kvp.Value));
- ParticipantTimelineDto.cs 领域类定义
public class ParticipantTimelineDto { public int ParticipantId { get; set; } public IEnumerable<ParticipantTimelineDelta> CsDiffPerMinDeltas { get; set; } public IEnumerable<ParticipantTimelineDelta> DamageTakenPerMinDeltas { get; set; } public string Role { get; set; } public IEnumerable<ParticipantTimelineDelta> DamageTakenDiffPerMinDeltas { get; set; } public IEnumerable<ParticipantTimelineDelta> XpPerMinDeltas { get; set; } public IEnumerable<ParticipantTimelineDelta> XpDiffPerMinDeltas { get; set; } public string Lane { get; set; } public IEnumerable<ParticipantTimelineDelta> CreepsPerMinDeltas { get; set; } public IEnumerable<ParticipantTimelineDelta> GoldPerMinDeltas { get; set; } }
- ParticipantTimelineDelta.cs 领域类定义
public class ParticipantTimelineDelta { public string Delta { get; set; } public double Value { get; set; } public ParticipantTimelineDelta() { } }
这套逻辑在未引入EF时运行完全正常,但现在我尝试为实体添加迁移以配置标识属性,却始终无法理清EF的映射规则。尝试创建迁移时,首先收到如下提示:
'ParticipantTimelineDelta' 与 'ParticipantTimelineDto' 之间存在多组未配置外键属性的关系,这将导致Entity Framework按照发现顺序在'ParticipantTimelineDelta'上创建命名依赖于发现顺序的影子属性。
这只是一个警告,我也理解触发这个警告的原因。但后续出现的实际报错才是最让我困惑的部分,报错内容如下:
实体类型'ParticipantTimelineDelta'需要定义主键。如果您打算使用无键实体类型,请在'OnModelCreating'方法中调用'HasNoKey'。有关无键实体类型的更多信息,可参考EF Core官方文档说明。
这个报错让我十分困惑,因为我确实已经定义了主键。我并不喜欢当前对delta数据的映射方式,但暂时也没有想到更好的实现方案。想请问:按照我当前的配置,是否有可能正确完成映射?如果无法实现,应当如何调整delta数据的映射方式?
以下是我为ParticipantTimeline和ParticipantTimelineDelta编写的EF配置:
- ParticipantTimelineConfiguration.cs 实体配置
public class ParticipantTimelineConfiguration : IEntityTypeConfiguration<ParticipantTimelineDto> { public void Configure(EntityTypeBuilder<ParticipantTimelineDto> builder) { builder.ToTable("ParticipantTimeline"); builder.HasKey(ps => ps.ParticipantId); builder.HasMany<ParticipantTimelineDelta>() .WithOne(); } }
- ParticipantTimelineDeltaConfiguration.cs 实体配置
public class ParticipantTimelineDeltaConfiguration : IEntityTypeConfiguration<ParticipantTimelineDelta> { public void Configure(EntityTypeBuilder<ParticipantTimelineDelta> builder) { builder.ToTable("ParticipantTimelineDeltas"); builder.HasKey(d => d.Delta); } }
我已经尝试在HasMany().WithOne()语句上配置HasForeignKey,但没有任何效果。
内容的提问来源于stack exchange,提问作者Nicolas Van Damme

