dotnet / ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.

Home Page:https://docs.microsoft.com/ef/ef6

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DBMigration.CreateStoredProcedure

nerid75 opened this issue · comments

If you create a stored procedure in a migration, you aren't able to create parameter with NULL as default value.

Steps to reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

        public override void Up()
        {
            CreateStoredProcedure(
                "dbo.sp_GetItemsForUser",
                p=> new
                {
                    user = p.String(maxLength: 200, defaultValue: null, defaultValueSql: null)
                },
                body: @"select a,b,c from myTable where @user is null or a=@user"
                );
        }

this code create the following stored procedure

create PROCEDURE [dbo].[sp_GetItemsForUser]
    @user [nvarchar](200) 
as
begin
select a,b,c from myTable where @user is null or a=@user
end

When you try to call the stored procedure from your code passing a null value to the parameter, you will get an exception because parameter cannot be null.

Further technical details

EF version: 6.2.0
Database Provider: EntityFramework.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 16.11.10

@nerid75 The parameter defaultValueSql is a string which will be used as the raw SQL default value. The fact you are setting this to null means nothing is set as the default value.
I think you need

        public override void Up()
        {
            CreateStoredProcedure(
                "dbo.sp_GetItemsForUser",
                p=> new
                {
                    user = p.String(maxLength: 200, defaultValueSql: "NULL")
                },
                body: @"select a,b,c from myTable where @user is null or a=@user"
                );
        }

Yes @CZEMacLeod, it works like this.

Thanks so much!

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.