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

Support value conversions in EFCore5+ #35

Open
msawczyn opened this issue Nov 2, 2020 · 2 comments
Open

Support value conversions in EFCore5+ #35

msawczyn opened this issue Nov 2, 2020 · 2 comments
Labels
enhancement-M New feature request. Tshirt size: M

Comments

@msawczyn
Copy link
Owner

msawczyn commented Nov 2, 2020

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#modelbuilder-api-for-value-comparers

@msawczyn msawczyn added the enhancement New feature or request label Nov 2, 2020
@veeman
Copy link

veeman commented Jan 27, 2021

Currently i implemented this ValueConversion interface with the support of custom attributes and reflection. Maybe the following code can be adopted in the next release.

FieldConverterAttribute constructor prototype (maybe there is already an apropate deifnition in EF for this):

public FieldConvertAttribute(Type targetType)

Custom OnModelCreatedImpl implementation:

partial void OnModelCreatedImpl(ModelBuilder modelBuilder)
{
    // retrieve needed interfaces
    var valueConverter = this.GetService<IValueConverterSelector>();

    // process attributes
    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        foreach (var property in entityType.GetProperties())
        {
            // check for field converters
            var attributes = property?.PropertyInfo?.GetCustomAttributes(typeof(FieldConvertAttribute), false);
            if (attributes?.FirstOrDefault() is FieldConvertAttribute convertAttribute)
            {
                var converterInfos = valueConverter.Select(property.ClrType, convertAttribute.TargetType);

                // select first converter if there is any
                if (converterInfos.DefaultIfEmpty().FirstOrDefault() is ValueConverterInfo converterInfo)
                {
                    // create and apply converter
                    var converter = converterInfo.Create();
                    property.SetValueConverter(converter);
                }
                else 
                {
                    // maybe look for another converter sources or throw an exception
                }
            }
        }
    }
}

This looks up the built in property converters defined in EFCore5 - ValueConverterSelector.cs

Usage in EFDesigner CustomAttribute field:

FieldConvert(typeof(string))

@msawczyn
Copy link
Owner Author

This is good stuff. Thanks!

@msawczyn msawczyn added enhancement-M New feature request. Tshirt size: M and removed enhancement New feature or request labels Feb 11, 2021
@msawczyn msawczyn transferred this issue from msawczyn/EFDesigner Aug 14, 2022
@msawczyn msawczyn added this to Enhancement Requests in EFDesigner2022 via automation Aug 14, 2022
@msawczyn msawczyn moved this from Enhancement Requests to Backlog in EFDesigner2022 Aug 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement-M New feature request. Tshirt size: M
Projects
Development

No branches or pull requests

2 participants