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

Customize Navigation_ naming convention for Owned Type columns #17152

Open
Tracked by #22952
spottedmahn opened this issue Aug 14, 2019 · 11 comments
Open
Tracked by #22952

Customize Navigation_ naming convention for Owned Type columns #17152

spottedmahn opened this issue Aug 14, 2019 · 11 comments

Comments

@spottedmahn
Copy link

Describe what is not working as expected

I would like a way to customize the Navigation_ part of the convention in owned types:

By convention, EF Core will name the database columns for the properties of the owned entity type following the pattern Navigation_OwnedEntityProperty. Therefore the StreetAddress properties will appear in the 'Orders' table with the names 'ShippingAddress_Street' and 'ShippingAddress_City'.
Source

Desired Behavior

In the above example, I want the StreetAddress properties to appear in the 'Orders' table as 'Street' and 'City'.

ShipsTo Example

It would simplify the sample code to change ShippingAddress_ to ShipsTo:

image

vs

modelBuilder.Entity<Order>().OwnsOne(
    o => o.ShippingAddress,
    prefix: "ShipsTo");

Thoughts? 💭

Further technical details

EF Core version: 2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10
IDE: 16.3 Preview 2

@spottedmahn
Copy link
Author

A better example is if I complete the ShippingAddress entity which is incomplete (doesn't need to be complete for the documentation, I get that 😊)

modelBuilder.Entity<Order>().OwnsOne(
    o => o.ShippingAddress,
    sa =>
    {
        sa.Property(p => p.Street).HasColumnName("ShipsToStreet");
        sa.Property(p => p.Street2).HasColumnName("ShipsToStreet2");
        sa.Property(p => p.City).HasColumnName("ShipsToCity");
        sa.Property(p => p.State).HasColumnName("ShipsToState");
        sa.Property(p => p.Zip).HasColumnName("ShipsToZip");
        sa.Property(p => p.Country).HasColumnName("ShipsToCountry");
    });

vs

modelBuilder.Entity<Order>().OwnsOne(
    o => o.ShippingAddress,
    prefix: "ShipsTo");

@ajcvickers
Copy link
Contributor

This is something that in the future could be a public convention. Backlogging for now.

@ajcvickers ajcvickers added this to the Backlog milestone Aug 16, 2019
@spottedmahn
Copy link
Author

Great 👍

If you can point me in the right direction maybe I could implement it 😊? Where do I start looking in the code to add this? Seems pretty straight forward on the surface.

@ajcvickers
Copy link
Contributor

@spottedmahn It is dependent on #214, which is a fairly substantial chunk of work and still requires some design.

@spottedmahn
Copy link
Author

I see, thanks! I'll circle back once #214 is complete (only 3 checkboxes to go 😊)

@AndriySvyryd AndriySvyryd changed the title Customize Navigation_ String for Owned Types Customize Navigation_ naming convention for Owned Type columns Sep 21, 2021
@AndriySvyryd
Copy link
Member

Related to #9329

@spottedmahn
Copy link
Author

#214 was closed 😊

@AndriySvyryd
Copy link
Member

On the nightly build or RC1 you can try adding your own convention. It would need to implement IForeignKeyOwnershipChangedConvention and IPropertyAddedConvention and perhaps some other interfaces. It's added in ConfigureConventions by calling Conventions.Add(_ => new MyConvention())

There isn't relevant documentation yet, see https://github.com/dotnet/efcore/blob/71b7f6dcc81a6285568305534ea7dd7e4edc7709/src/EFCore/Metadata/Conventions/KeyDiscoveryConvention.cs for an example

@timmi4sa
Copy link

@AndriySvyryd @ajcvickers Gentlemen this inability to set up an existing table to have an Owned Type property stops our porting away from EF6 to EF Core. We are heavy users of Complex Types, and in the EDMX model (which was amazing for the teams of all sizes by the way!) we were able to "map" a collection of existing columns "as they were" into a Complex Type. I understand that Owned Types have other useful functionality but there is something important that has been taken away! Please consider implementing a capability to allow for a custom prefix (or lack thereof!) Thank you!

@kawazoe
Copy link

kawazoe commented Sep 22, 2023

Same. My team have just hit the limits of owned type on Postgres due to limitations of column name sizes. We have so many nested properties that we are starting to get ambiguous column name errors because it cannot distinguish between different props of our types.

@jesshannon
Copy link

I've just wasted hours on this problem. Wish I'd found this thread earlier.

Can't believe this still isn't fixed, makes adding Owned types with a legacy database impossible.

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

6 participants