© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
.eax

❔ EF Core many-to-many migration problems

I added a new entity
VesselTransfer
VesselTransfer
and added two one-to-many relationship to the
Vessel
Vessel
entity, and added the configuration as shown in the code below to
OnModelCreating
OnModelCreating
.
No problems with creating the migration and applying it to the DB, but now I'm getting this error during runtime
Unable to determine the relationship represented by navigation 'Team.Persons' of type 'ICollection<Person>'.
Unable to determine the relationship represented by navigation 'Team.Persons' of type 'ICollection<Person>'.
. On a entity and relationship that has no changes, and I'm getting some form of this exception on whatever query I run. The error has different entities and relationships depending on the query.

I'm a bit of a loss on what is wrong as nothing seems wrong from the configurations.

public class VesselTransfer : BaseTrackedEntity
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }

    [IsProjected] public int DropOffVesselId { get; set; }
    public virtual Vessel DropOffVessel { get; set; }

    [IsProjected] public int CollectVesselId { get; set; }
    public virtual Vessel CollectVessel { get; set; }
}

public class Vessel : BaseEntityVortex
{
    public string Name { get; set; }

    public virtual ICollection<VesselTransfer> VesselTransferDropOffs { get; set; }
    public virtual ICollection<VesselTransfer> VesselTransferCollections { get; set; }
}

// In db context
modelBuilder.Entity<Vessel>()
    .HasMany(x => x.VesselTransferDropOffs)
    .WithOne(x => x.DropOffVessel)
    .HasForeignKey(x => x.DropOffVesselId)
    .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Vessel>()
    .HasMany(x => x.VesselTransferCollections)
    .WithOne(x => x.CollectVessel)
    .HasForeignKey(x => x.CollectVesselId)
    .OnDelete(DeleteBehavior.Restrict);
public class VesselTransfer : BaseTrackedEntity
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }

    [IsProjected] public int DropOffVesselId { get; set; }
    public virtual Vessel DropOffVessel { get; set; }

    [IsProjected] public int CollectVesselId { get; set; }
    public virtual Vessel CollectVessel { get; set; }
}

public class Vessel : BaseEntityVortex
{
    public string Name { get; set; }

    public virtual ICollection<VesselTransfer> VesselTransferDropOffs { get; set; }
    public virtual ICollection<VesselTransfer> VesselTransferCollections { get; set; }
}

// In db context
modelBuilder.Entity<Vessel>()
    .HasMany(x => x.VesselTransferDropOffs)
    .WithOne(x => x.DropOffVessel)
    .HasForeignKey(x => x.DropOffVesselId)
    .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Vessel>()
    .HasMany(x => x.VesselTransferCollections)
    .WithOne(x => x.CollectVessel)
    .HasForeignKey(x => x.CollectVesselId)
    .OnDelete(DeleteBehavior.Restrict);
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ EF Core duplicating many-to-many?
C#CC# / help
4y ago
Migration issues -EF core.
C#CC# / help
16mo ago
EF Core many-to-many relation error
C#CC# / help
4y ago
EF-Core unable to create migration
C#CC# / help
2y ago