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

Configuring a value converter for a property of an owned type #13485

Closed
BrightSoul opened this issue Oct 3, 2018 · 5 comments
Closed

Configuring a value converter for a property of an owned type #13485

BrightSoul opened this issue Oct 3, 2018 · 5 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@BrightSoul
Copy link

BrightSoul commented Oct 3, 2018

Hi all, I'using EF Core 2.1 and I have this value object in my model, which is then configured as an owned type for properties such as "Price".

public class Money 
{
    public Currency Currency { get; set; }
    public decimal Amount { get; set; }
}

Currency is a simple enum like this.

public enum Currency
{
  EUR,
  USD,
  GBP
}

I'd like to persist the Currency property as a string, i.e. I want 'EUR' or 'USD' to appear in the database instead of its numerical representation.

For this, I tried to configure the EnumToStringConverter but the fluent interface doesn't seem to allow that. Things I've tried.

modelBuilder.Entity<MyEntity>(e => e.Price.Currency).HasConversion... //Doesn't allow complex property expression
modelBuilder.Owned<Money>()... //Doesn't have a HasConversion method.
modelBuilder.Entity<Money>()... //Doesn't work since this is an owned type and not an entity

How should I do it? I don't want to write a converter for the whole Money object.
Thanks in advance.

@BrightSoul BrightSoul changed the title Configuring a ValueConverter for an owned type property. Configuring a value converter for an owned type property. Oct 3, 2018
@BrightSoul BrightSoul changed the title Configuring a value converter for an owned type property. Configuring a value converter for a property of an owned type Oct 3, 2018
@Tarig0
Copy link

Tarig0 commented Oct 3, 2018

think you need

Entity<MyEntity>().Property(e => e.Price.Currency).HasConversion

@ajcvickers
Copy link
Member

ajcvickers commented Oct 3, 2018

@Tarig0 I'd be really surprised if that works. Did you try it?

@ajcvickers
Copy link
Member

@BrightSoul This should work:

modelBuilder.Entity<Blog>(c =>
{
    c.OwnsOne(e => e.Price, b =>
    {
        b.Property(e => e.Currency).HasConversion<string>();
    });
});

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Oct 3, 2018
@BrightSoul
Copy link
Author

BrightSoul commented Oct 3, 2018

Thank you @ajcvickers, it's working wonderfully. I have to repeat that configuration with each usage of the Money type but it's not a big deal right now. Of course, a type-wide converter configuration would be even more useful. Something like this:

modelBuilder.Owned<Money>().Property(money => money.Currency).HasConversion<string>();

Also, I couldn't find any mention in the documentation, maybe a paragraph about owned types would help.
Thanks again!

@ajcvickers
Copy link
Member

@BrightSoul I'll file a docs issue. Global config of a converter is tracked by #10784.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants