Repository: ErikEJ/EFCorePowerTools

Issue #2937: Add Proper Nullable support to Procedure Definitions
Created: 2025-04-10T16:02:49Z
What problem are you trying to solve?
Back in #2261, it was made that the generated code for Stored Procedures was always set to Nullable disable.
However, the real solution should be (imo) properly supporting nullable. If "use-nullable-reference-types" is set to true, then the parameters for generated Sprocs should augmented with the ? so that they are classified as nullable and the "#nullable disable" should not be part of the Procedures class.
eg:
 internal partial class MyDbContextProcedures : IMyDbContextProcedures
{
    private readonly MyDbContext _context;

    public MyDbContextProcedures(MyDbContext context)
    {
        _context = context;
    }

    public virtual async Task<List<GetMyWidgetResult>> GetMyWidgetAsync(int? id, OutputParameter<int>? returnValue = null, CancellationToken? cancellationToken = default)
    {
        var parameterreturnValue = new SqlParameter
        {
            ParameterName = "returnValue",
            Direction = System.Data.ParameterDirection.Output,
            SqlDbType = System.Data.SqlDbType.Int,
        };

        var sqlParameters = new []
        {
            new SqlParameter
            {
                ParameterName = "Id",
                Value = id ?? Convert.DBNull,
                SqlDbType = System.Data.SqlDbType.Int,
            },
            parameterreturnValue,
        };
        var _ = await _context.SqlQueryAsync<GetMyWidgetResult>("EXEC @returnValue = [dbo].[GetMyWidget] @id = @id", sqlParameters, cancellationToken);

        returnValue?.SetValue(parameterreturnValue.Value);

        return _;
    }
}
specifically this line would change:
    public virtual async Task<List<GetMyWidgetResult>> GetMyWidgetAsync(int? id, OutputParameter<int> returnValue = null, CancellationToken cancellationToken = default)
to
    public virtual async Task<List<GetMyWidgetResult>> GetMyWidgetAsync(int? id, OutputParameter<int>? returnValue = null, CancellationToken? cancellationToken = default)
The main reason is that right now if the rest of your project uses NRTs, then this shows up as a warning:
ISearchDbContextProcedures procs; // injected
var result = await procs.GetMyWidgetAsync(1);
var first = result.SingleOrDefault(); // Warning since it can't know that result will never be null due to nullable disable being set. 
You can obviously use the null forgiveness operator (!), but that's not the best practice if it can be avoided.
Describe the solution you'd like
No response

Duplicate issue #2948: Store procedure  Add nullable in table definition
Created: 2025-05-03T15:21:37Z
Bug description / exact reproduction steps
hello
when you add the store procedure in your reverse engineer  le pragma #nullable disable is forgotten in the genration of the table ?
Your code
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
and 
with store procedure 

using System;
using System.Collections.Generic;
Stack traces


EF Core Power Tools version
2.6.961
EF Core Power Tools CLI version
2.6.961
Database provider
prostgresql
Target framework
No response
IDE
No response
Additional features in use

 T4 templates
 SQL Server .dacpac
 Handlebars template

Closing comment (2025-05-04T05:17:39Z): Is this a duplicate of #2937 ?