efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Customize Navigation_ naming convention for Owned Type columns

Open spottedmahn opened this issue 6 years ago • 8 comments

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 avatar Aug 14 '19 17:08 spottedmahn

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");

spottedmahn avatar Aug 14 '19 17:08 spottedmahn

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

ajcvickers avatar Aug 16 '19 18:08 ajcvickers

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.

spottedmahn avatar Aug 17 '19 01:08 spottedmahn

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

ajcvickers avatar Aug 19 '19 16:08 ajcvickers

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

spottedmahn avatar Aug 19 '19 16:08 spottedmahn

Related to https://github.com/dotnet/efcore/issues/9329

AndriySvyryd avatar Sep 21 '21 17:09 AndriySvyryd

#214 was closed 😊

spottedmahn avatar Aug 08 '22 13:08 spottedmahn

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

AndriySvyryd avatar Aug 09 '22 01:08 AndriySvyryd

@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!

timmi4sa avatar Aug 25 '23 14:08 timmi4sa

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.

kawazoe avatar Sep 22 '23 21:09 kawazoe