Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoInclude property of owned entities #34292

Closed
JonaBrackenwood opened this issue Jul 26, 2024 · 6 comments
Closed

AutoInclude property of owned entities #34292

JonaBrackenwood opened this issue Jul 26, 2024 · 6 comments

Comments

@JonaBrackenwood
Copy link

I want to be able to AutoInclude a property of an owned entity type.

Currently I have three tables that all have an owned Address.

builder.Entity<Customers>().OwnsOne(x => x.Address).HasOne(x => x.Country).WithMany();
builder.Entity<Organizations>().OwnsOne(x => x.Address).HasOne(x => x.Country).WithMany();
builder.Entity<Supplier>().OwnsOne(x => x.Address).HasOne(x => x.Country).WithMany();

This Address includes a Country, which I must include everywehere that the address is used.

I want to be able to include this Country automatically, so these includes everywhere:

context.Customers.Include(x => x.Address.Country)
context.Organizations.Include(x => x.Address.Country)
context.Suppliers.Include(x => x.Address.Country)

Can be replaced by one global AutoInclude:

modelBuilder.Entity<Address>().Navigation(x => x.Country).AutoInclude();

Currently this just produces the error The entity type 'Address' cannot be configured as non-owned because it has already been configured as a owned. If you want to override previous configuration first remove the entity type from the model by calling 'Ignore'.

@roji
Copy link
Member

roji commented Jul 26, 2024

modelBuilder.Entity<Address>()

This configures Address as its own, separate non-owned entity type (i.e. with its own table), which conflicts with the other cnofiguration of it as owned - that's why you're getting the error.

Have you considered simply configuring Address itself as Owned where its used, as opposed to using AutoInclude?

@JonaBrackenwood
Copy link
Author

modelBuilder.Entity()

This configures Address as its own, separate non-owned entity type (i.e. with its own table), which conflicts with the other cnofiguration of it as owned - that's why you're getting the error.

Have you considered simply configuring Address itself as Owned where its used, as opposed to using AutoInclude?

It's already configured as owned and that's why I'm getting the exception, and that is all working correctly. I just want to AutoInclude a property on the owned type which you can't do at the moment.

@roji
Copy link
Member

roji commented Jul 26, 2024

Sorry, I meant to ask why not configure Country as owned (as opposed to auto-include)?

@JonaBrackenwood
Copy link
Author

In this case Country is a list of possible/configured countries and also has relations to other entity types, so it can't be only owned by address. Wouldn't that just make it functionally the same as a string property named Country?

@roji
Copy link
Member

roji commented Jul 26, 2024

Understood - no, if you want Country to be in its own table with addresses referencing it by foreign key, and joins every time you load it, this is indeed the right modeling; the alternative would be to indeed just have it as a string column with possibly a check constraint, this can be modeled either as a simple property or as an owned entity/complex type.

In any case, I don't think there's currently a way to configure all navigations pointing to an entity type to be auto-loading (which is your original request) - @AndriySvyryd @ajcvickers does this ring any bells?

@AndriySvyryd
Copy link
Member

This is essentially a duplicate of #2953

@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants