Skip to content

Order of relationship vs navigation configuration when using ApplyConfigurationsFromAssembly #30068

Closed as duplicate

Description

@vflame

There are times when we have to define the same relationship in two places (when we use ApplyConfigurationsFromAssembly) since the order of applying IEntityTypeConfiguration matters.

When we explicitly control the order of configuration, it works as expected (e.g., explicitly configured in ApplicationDbContext.cs OnModelCreating).

But if we using something like the following two config classes, the order in which the configuration is applied can cause the navigation not found exception to be thrown if PaymentConfiguration was applied before OrderConfiguration.

public class PaymentConfiguration : IEntityTypeConfiguration<Payment>
{
    public void Configure(EntityTypeBuilder<Payment> builder)
    {
        //navigation not found exception thrown
        builder.Navigation("_order").AutoInclude();
    }
}

public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
       builder.HasMany<Payment>("_payments").WithOne("_order");
    }
}

Ideally, the order of applying configuration should not matter as long as the relationship is fully defined at some point.

Activity

  1. ajcvickers commented on Jan 14, 2023

    @ajcvickers
    Contributor

    @vflame Why not define the relationship in PaymentConfiguration?

    builder.HasOne<Order>("_order").WithMany("_payments");
    builder.Navigation("_order").AutoInclude();
  2. vflame commented on Jan 14, 2023

    @vflame
    Author

    @vflame Why not define the relationship in PaymentConfiguration?

    builder.HasOne<Order>("_order").WithMany("_payments");
    builder.Navigation("_order").AutoInclude();

    That's what we resorted to doing. Just wanted to point out this behavior and how order of applying configs matters when ideally it shouldn't. (e.g., all navigations configs are applied after relationship configs are applied) after scanning all assemblies.

  3. ajcvickers commented on Jan 19, 2023

    @ajcvickers
    Contributor

    @AndriySvyryd I can't find an issue for ordering the configurations.

  4. AndriySvyryd commented on Jan 19, 2023

    @AndriySvyryd
    Member

    Duplicate of #22035

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions