❔ Ef core creating shadow foreign key, why?

Got this error:
The foreign key property 'LessonPhase.LessonId1' was created in shadow state because a conflicting property with the simple name 'LessonId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type
The foreign key property 'LessonPhase.LessonId1' was created in shadow state because a conflicting property with the simple name 'LessonId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type
The classes:
public class LessonPhase : Entity<Guid>, ISoftDelete
{
public required int PhaseNumber { get; init; }
public int NumberOfExplanationRequestsMade { get; set; }
public Lesson Lesson { get; init; }
public Guid LessonId { get; init; }
public bool IsDeleted { get; set; }
public class LessonPhase : Entity<Guid>, ISoftDelete
{
public required int PhaseNumber { get; init; }
public int NumberOfExplanationRequestsMade { get; set; }
public Lesson Lesson { get; init; }
public Guid LessonId { get; init; }
public bool IsDeleted { get; set; }
public class Lesson : FullAuditedAggregateRootWithUser<Guid, IdentityUser>
{
public required string LessonSrcLanguage { get; init; }
public required string LessonDstLanguage { get; init; }
public int LessonNumber { get; set; }
public int LessonDifficulty { get; init; }
public required LessonState LessonState { get; set; }
public ICollection<LessonPhase> Phases { get; set; }
public LessonPhase CurrentPhase { get; set; }

public Lesson()
{
Phases = new Collection<LessonPhase>();
}
}
public class Lesson : FullAuditedAggregateRootWithUser<Guid, IdentityUser>
{
public required string LessonSrcLanguage { get; init; }
public required string LessonDstLanguage { get; init; }
public int LessonNumber { get; set; }
public int LessonDifficulty { get; init; }
public required LessonState LessonState { get; set; }
public ICollection<LessonPhase> Phases { get; set; }
public LessonPhase CurrentPhase { get; set; }

public Lesson()
{
Phases = new Collection<LessonPhase>();
}
}
Mapping:
builder.Entity<Lesson>(b =>
{
b.ToTable(lingumindConsts.DbTablePrefix + "Lessons", lingumindConsts.DbSchema);
b.ConfigureByConvention(); //auto configure for the base class props
});

builder.Entity<LessonPhase>(b =>
{
b.ToTable(lingumindConsts.DbTablePrefix + "Phases", lingumindConsts.DbSchema);
b.HasOne(x => x.Lesson).WithMany(x => x.Phases).HasForeignKey(x => x.LessonId);
builder.Entity<Lesson>(b =>
{
b.ToTable(lingumindConsts.DbTablePrefix + "Lessons", lingumindConsts.DbSchema);
b.ConfigureByConvention(); //auto configure for the base class props
});

builder.Entity<LessonPhase>(b =>
{
b.ToTable(lingumindConsts.DbTablePrefix + "Phases", lingumindConsts.DbSchema);
b.HasOne(x => x.Lesson).WithMany(x => x.Phases).HasForeignKey(x => x.LessonId);
Any idea? Cant seem to resolve this
10 Replies
jcotton42
jcotton422y ago
what if you remove the manual config? and let EF infer by convetion?
antimatter8189
antimatter8189OP2y ago
Why do that tho? In addition, could this cause the error?
public ICollection<LessonPhase> Phases { get; set; }
public LessonPhase CurrentPhase { get; set; }
public ICollection<LessonPhase> Phases { get; set; }
public LessonPhase CurrentPhase { get; set; }
Those are on the Lesson class, someone put the Class as a single reference and a collection, could it be the root cause?
jcotton42
jcotton422y ago
generally I prefer to let EF infer by convention, only doing fluent when conventions do not suffice it makes the code easier to understand, and also keeps you from misconfiguring your db by accident as for the single ref and collection, I'm not sure I haven't tried a setup like that before
antimatter8189
antimatter8189OP2y ago
Well after removing said line i get:
Unable to determine the relationship represented by navigation 'LessonPhase.Lesson' of type 'Lesson'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Unable to determine the relationship represented by navigation 'LessonPhase.Lesson' of type 'Lesson'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
So basically gotta go fluent on this
jcotton42
jcotton422y ago
ah rip you might want to move to #database EF is something I'm not great on helping with unless it's basic, or I can be more hands on
antimatter8189
antimatter8189OP2y ago
can you tag some ef core pro here perhaps?
jcotton42
jcotton422y ago
the pros hang out in #database just ask there
antimatter8189
antimatter8189OP2y ago
Im thinking those lines are the root cause, just want to confirm before gutting the codebase Alright Appreciate your time, have a great day @jcotton42
jcotton42
jcotton422y ago
np
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?