Skip to content

add HasVogenConversion for nullable PropertyBuilder<TValueObject> - #886

Open
apphost wants to merge 1 commit into
SteveDunn:mainfrom
apphost:feature-efcore-nullable
Open

apphost wants to merge 1 commit into
SteveDunn:mainfrom
apphost:feature-efcore-nullable

Conversation

@apphost

@apphost apphost commented Feb 10, 2026

Copy link
Copy Markdown

add HasVogenConversion for nullable PropertyBuilder
add HasVogenConversion for ComplexTypePropertyBuilder

#873

add HasVogenConversion for ComplexTypePropertyBuilder
@SteveDunn

Copy link
Copy Markdown
Owner

Hi, thanks for the PR. I'll try to help get it working soon, sorry for the slow feedback, I get very little time for Vogen recently

__ACCESSIBILITY_OF_MARKER_CLASS__ static global::Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<VOTYPE> HasVogenConversion(this global::Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<VOTYPE> propertyBuilder) =>
propertyBuilder.HasConversion<__GENERATED_CONVERTER_NAME__, __GENERATED_COMPARER_NAME__>();
__ACCESSIBILITY_OF_MARKER_CLASS__ static global::Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<VOTYPE?> HasVogenConversion(this global::Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<VOTYPE?> propertyBuilder) =>
propertyBuilder.HasConversion<__GENERATED_CONVERTER_NAME__, __GENERATED_COMPARER_NAME__>();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't considered an overload in C#, hence the compiler errors.

@aminmashayekhi

Copy link
Copy Markdown

i add this Extension for handle this... is it ok?

public static class VogenEfExtensions
{
    public static PropertyBuilder<T?> HasVogenConversion<T>(this PropertyBuilder<T?> builder)
        where T : struct
    {
        var type = typeof(T);
        var fromMethod = type.GetMethod("From", BindingFlags.Public | BindingFlags.Static)
                         ?? throw new InvalidOperationException($"{type.FullName} does not have a public static From method.");
        var valueProp = type.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance)
                        ?? throw new InvalidOperationException($"{type.FullName} does not have a public instance Value property.");

        var converter = new ValueConverter<T?, object?>(
            model => model == null ? null : valueProp.GetValue(model.Value),
            provider => provider == null ? (T?)null : (T)fromMethod.Invoke(null, new[] { provider })
        );

        return builder.HasConversion(converter);
    }

    public static PropertyBuilder<T> HasVogenConversion<T>(this PropertyBuilder<T> builder)
        where T : struct
    {
        var type = typeof(T);
        var fromMethod = type.GetMethod("From", BindingFlags.Public | BindingFlags.Static)
                         ?? throw new InvalidOperationException($"{type.FullName} does not have a public static From method.");
        var valueProp = type.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance)
                        ?? throw new InvalidOperationException($"{type.FullName} does not have a public instance Value property.");

        var converter = new ValueConverter<T, object?>(
            model => valueProp.GetValue(model),
            provider => (T)fromMethod.Invoke(null, new[] { provider })
        );

        return builder.HasConversion(converter);
    }
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants