© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•7mo ago
Nasdack

EF Core Nullable Foreign Key Relationship

        x.HasOne(t => t.Client)
            .WithMany()
            .HasForeignKey(f => f.ClientId)
            .OnDelete(DeleteBehavior.Restrict)
            .IsRequired(false);
        x.HasOne(t => t.Client)
            .WithMany()
            .HasForeignKey(f => f.ClientId)
            .OnDelete(DeleteBehavior.Restrict)
            .IsRequired(false);

I have this one to many
FK
FK
relationship configuration in EF Core that I want to be nullable. If I set the type of
ClientId
ClientId
to be nullable (which is a custom class that wraps up a
Guid
Guid
value which the entire codebase uses), EF Core will complain about:
System.InvalidOperationException: The client projection contains a reference to a constant expression of 'Common.Identifier'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead.
System.InvalidOperationException: The client projection contains a reference to a constant expression of 'Common.Identifier'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead.

with the generated pre-compilation query being:
        ClientId = namelessParameter{0}.ClientId == null ? 00000000-0000-0000-0000-000000000000 : new Identifier{ Value = (Guid)namelessParameter{0}.ClientId.Value }
        ClientId = namelessParameter{0}.ClientId == null ? 00000000-0000-0000-0000-000000000000 : new Identifier{ Value = (Guid)namelessParameter{0}.ClientId.Value }

EF Core is doing a ternary conditional operation and is initializing
Identifier
Identifier
to its default constant value when not set, which is causing this issue. I don't know how to instruct it not to do that.

Right now I cannot mess with the custom
Identifier?
Identifier?
class so using
Guid?
Guid?
instead is not an ideal option. Using
IsRequired(false)
IsRequired(false)
instead of
Nullable<>
Nullable<>
also brought no results. I have also tried this converter:
public sealed class IdentifierConverter() : ValueConverter<Identifier, Guid>(id =>      id.Value,
    value => value);
public sealed class IdentifierConverter() : ValueConverter<Identifier, Guid>(id =>      id.Value,
    value => value);

    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder
            .Properties<Identifier>()
            .HaveConversion<IdentifierConverter>();
    }
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder
            .Properties<Identifier>()
            .HaveConversion<IdentifierConverter>();
    }

to no avail, even with a nullable version.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ ef core foreign key problem
C#CC# / help
4y ago
✅ Setting Foreign key as Primary Key (EF Core)
C#CC# / help
2y ago
❔ Ef core creating shadow foreign key, why?
C#CC# / help
3y ago
✅ EF Core Relationship
C#CC# / help
2y ago