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

Nullable object must have a value error when I try to proyect a nullable navigation property #32181

Closed
ComptonAlvaro opened this issue Oct 30, 2023 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ComptonAlvaro
Copy link

Hello:

I am using Entity Core 7.

I have this model:


public class Document
{
    public long Id;
    public DocumentCategory? Category;

}

public class DocumentCategory
{
    public long Id;
    public string Name;
}

I would like to do this query:

private Expression<Func<Document, Document>> GetProjeciton()
{
    return x => new Document
    {
        Id = x.Id,
        Category = new DocumentCategory()
        {
            Id = x.Category.Id,
            Name = x.Category.Name,
        }
    };
}


return await _context.Documents
    .AsNoTracking()
    .Select(GetProjeciton())
    .Where(x => x.Id == 1)
    .ToListAsync();

But I get an exception: "Nullable object must have a value."

I have check the T-SQL the database receives, and it is correct, it is left join, but it seems that when EF Core tries to create the objects, it fails.

I have another properties that can't be null so the databse always return a value and it works as expected.

How could set the category in the projection?

Thanks.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2023
@ComptonAlvaro
Copy link
Author

In this issue they give the solution: #22517

I have to add a check if it is null in this way:

        Catetogy = x.Category == null ? null : new DocumentCategory()
        {
            Id = x.Category.Id,
            Name= x.Category.name,
        },

Thanks.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Oct 30, 2023
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

2 participants