Skip to content

C# Source generator for EF Core required navigation properties

License

Notifications You must be signed in to change notification settings

KeterSCP/EfNavGuard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EfNavGuard

C# source generator to generate guards for Entity Framework Core required navigation properties. Basically, it generates a boilerplate code presented in https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#required-navigation-properties.

Warning

To use this generator, your project must have C# version 13.0 (.NET 9) or higher.

Installation

  • Modify your .csproj file by adding new PackageReference:
<ItemGroup>
    <PackageReference Include="EfNavGuard" Version="1.0.0" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

Usage

Consider the following model:

public class MainEntity
{
    public int Id { get; init; }
    
    public int OtherEntityId { get; init; }
    public OtherEntity OtherEntity { get; set; }
}

public class OtherEntity
{
    public int Id { get; set; }
}

Above code will produce warning if project has NRT enabled. To fix it with EfNavGuard, you need to apply following changes:

  • Mark MainEntity class and OtherEntity property as partial (this is required, so the source generator can augment the class).
  • Apply GuardedNavigation attribute to OtherEntity property.

So the final code will look like this:

using EfNavGuard;

public partial class MainEntity
{
    public int Id { get; init; }
    
    public int OtherEntityId { get; init; }
    [GuardedNavigation]
    public partial OtherEntity OtherEntity { get; set; }
}

public class OtherEntity
{
    public int Id { get; init; }
}

Now the warning is gone. And if someone tries to access OtherEntity property without loading it, exception will be thrown.

Under the hood, the source generator will generate following code:

// <auto-generated>
// This code was generated by a tool, any changes made to it will be lost.
// </auto-generated>
#nullable enable
partial class MainEntity
{
    private global::OtherEntity? _OtherEntity;

    public partial global::OtherEntity OtherEntity
    {
        get => _OtherEntity ?? throw new System.InvalidOperationException("OtherEntity was not loaded. Make sure to include it in the query.");
        set => _OtherEntity = value;
    }
}

About

C# Source generator for EF Core required navigation properties

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages