© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
sl8er

EF Core Many-to-One (Many Owned Entity to Non-owned Entity)

In the context of a multi-tenant application, I want to design an aggregate like so - a root
Customer
Customer
has a
List<Address>
List<Address>
,
Address
Address
being an owned type. A
Customer
Customer
also references a
Tenant
Tenant
. If I also want
Address
Address
to reference a
Tenant
Tenant
, it seems EF Core isn't happy when trying to configure a
Tenant
Tenant
like so:

// This is fine.
builder
    .HasMany<Customer>()
    .WithOne(x => x.Tenant)
    .HasForeignKey("tenant_id")
    .IsRequired()
    .OnDelete(DeleteBehavior.Restrict);

// This throws - System.InvalidOperationException: The entity type 'Address' cannot be configured as non-owned because it has already been configured as a owned. Use the nested builder in `OwnsOne` or `OwnsMany` on the owner entity type builder to further configure this type. If you want to override previous configuration first remove the entity type from the model by calling 'Ignore'.
builder
    .HasMany<Address>()
    .WithOne(x => x.Tenant)
    .HasForeignKey("tenant_id")
    .IsRequired()
    .OnDelete(DeleteBehavior.Restrict);
// This is fine.
builder
    .HasMany<Customer>()
    .WithOne(x => x.Tenant)
    .HasForeignKey("tenant_id")
    .IsRequired()
    .OnDelete(DeleteBehavior.Restrict);

// This throws - System.InvalidOperationException: The entity type 'Address' cannot be configured as non-owned because it has already been configured as a owned. Use the nested builder in `OwnsOne` or `OwnsMany` on the owner entity type builder to further configure this type. If you want to override previous configuration first remove the entity type from the model by calling 'Ignore'.
builder
    .HasMany<Address>()
    .WithOne(x => x.Tenant)
    .HasForeignKey("tenant_id")
    .IsRequired()
    .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 Many to Many relationship through Entity
C#CC# / help
14mo ago
❔ EF Core duplicating many-to-many?
C#CC# / help
4y ago
❔ EF Core many-to-many migration problems
C#CC# / help
3y ago