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

FluentAPI configuration PrimitiveCollection on OwnedNavigationBuilder returns PropertyBuilder instead of PrimitiveCollectionBuilder #32601

Closed
MJLHThomassenHadrian opened this issue Dec 13, 2023 · 2 comments · Fixed by #32753
Labels
area-model-building breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@MJLHThomassenHadrian
Copy link

MJLHThomassenHadrian commented Dec 13, 2023

When configuring an owned type, if that owned type has a primitive collection, configuring it using the new PrimitiveCollection on the builder does not alow me to specify .ElementType() because a PropertyBuilder is returned instead of a PrimitiveCollectionBuilder. I need this to convert a list of enums to strings (instead of having the enums as numbers in the database).

Include your code

Simplified example.

Entity definition:

public class Foo
{
    public Guid Id { get; set; }
    public OwnedBar Bar { get; set; }
}

public class OwnedBar
{
    public List<SomeEnum> MyPrimitiveCollection { get; set; }
}

Configuration:

public class FooConfiguration : IEntityTypeConfiguration<Foo>
{
    public void Configure(EntityTypeBuilder<Foo> builder)
    {
        builder.HasKey(x => x.Id);

        builder.OwnsOne(x => x.Bar)
            .PrimitiveCollection(x => x.MyPrimitiveCollection)
            .ElementType() // <-- This is missing
            .HasConversion<string>();
    }
}

Include provider and version information

EF Core version: 8.0.0
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 8.0
Operating system: Windows 11
IDE: VS Code

Update 2023-12-20: Included information that the list is an list of enum members instead of ints.

@MJLHThomassenHadrian
Copy link
Author

MJLHThomassenHadrian commented Dec 20, 2023

Any updates on this? I have a workaround currently, but would be nice to apply this in our system.

My current workaround is configuring like this:

builder.OwnsOne(x => x.Bar)
    .Property(x => x.MyPrimitiveCollection)
    .HasConversion<MyPrimitiveCollectionConverter>()
    .HasColumnType("text[]");

And defining MyPrimitiveCollectionConverter:

using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

class MyPrimitiveCollectionConverter: ValueConverter<List<SomeEnum>, List<string>>
{
        public MyPrimitiveCollectionConverter()
            : base(x => ToStringList(x), s => ToEnumList(s))
        {
        }

        private static List<string> ToStringList(List<SomeEnum> enumList)
        {
            return enumList.Select(x => x.ToString()).ToList();
        }

        private static List<SomeEnum> ToEnumList(List<string> stringList)
        {
            return stringList.Select(x => x.ToEnum<SomeEnum>()).ToList();
        }
}

Note that SomeEnum is an enum thats defined somewhere, didn't include that in the example.

@ajcvickers
Copy link
Member

/cc @AndriySvyryd

@ajcvickers ajcvickers added this to the 8.0.x milestone Jan 4, 2024
AndriySvyryd added a commit that referenced this issue Jan 9, 2024
…dentEntity>.PrimitiveCollection<TProperty> to PrimitiveCollectionBuilder<TProperty>

Fixes #32601
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 9, 2024
@AndriySvyryd AndriySvyryd removed their assignment Jan 9, 2024
AndriySvyryd added a commit that referenced this issue Jan 10, 2024
…dentEntity>.PrimitiveCollection<TProperty> to PrimitiveCollectionBuilder<TProperty> (#32753)

Fixes #32601
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.2 Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants