koenbeuk / EntityFrameworkCore.Projectables

Project over properties and functions in your linq queries
MIT License
260 stars 17 forks source link

Extension method not found when NullConditionalRewriteSupport is configured #65

Closed buraktamturk closed 1 year ago

buraktamturk commented 1 year ago

Hello, thanks for such an amazing library again.

The build fails with

'vfile' does not contain a definition for 'ToVFile' and no accessible extension method 'ToVFile' accepting a first argument of type 'vfile' could be found (are you missing a using directive or an assembly reference?)"

when using null propagation. Example code:

public static class ContentExtensions
{
    [Projectable]
    public static VFile ToVFile(this vfile a) => new VFile
    {
        id = a.id
        // ...
    };

    [Projectable] // works when ? not used, fails when ? is used (this is intentional behaviour)
    [Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)] // fails with the above error when ? is used
    [Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)] // fails with the same error when ? is used
    public static Content ToContent(this content ms) => new Content
    {
        featured_image = ms.featured_image?.ToFile()
    };
}

My guess is that the source generator does not qualify the assembly and the class correctly when handling null conditions.

Thanks a lot!

koenbeuk commented 1 year ago

The build failure seems unrelated to what this library is doing. (Also note that the code sample is broken).

This works for me:

public class VFileDTO
{
    public int id { get; set; }
}

public class VFile
{
    public int id { get; set; }
}

public class ContentDTO
{
    public VFileDTO? featured_image { get; set; }
}

public class Content
{
    public VFile? featured_image { get; set; }
}

public static class ContentExtensions
{
    [Projectable]
    public static VFile ToVFile(this VFileDTO a) => new VFile {
        id = a.id
        // ...
    };

    [Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)] // fails with the above error when ? is used
    public static Content ToContent(this ContentDTO ms) => new Content {
        featured_image = ms.featured_image?.ToVFile()
    };
}
buraktamturk commented 1 year ago

Oh, perhaps the problem occurs when the models are in different projects. Like this:

Screenshot 2023-03-17 at 12 30 24 AM

Commenting the other code fixes the build issue:

Screenshot 2023-03-17 at 12 31 26 AM
buraktamturk commented 1 year ago

Sample project: Archive.zip

koenbeuk commented 1 year ago

Good catch, this got introduced with the release of V3, in particular by this PR: https://github.com/koenbeuk/EntityFrameworkCore.Projectables/pull/49

ExtensionMethods are not getting fully qualified if any of the parents within the hierarchy are of ConditionalAccessExpressions.

I'll work on a fix in the coming days.