Alter Stored Procedure in DB Migration EF 6 Code F

2019-06-24 12:19发布

问题:

I am using an Empty Migration to update a stored procedure in my database. The stored procedure is a custom stored proc that was added in the intial creation of the database.

I have discovered the 'AlterStoredProcedure' method in the DbMigration class and this works to update the stored procedure, however I have to pass through the parameters of the stored procedure and I want to set the default value of a boolean and some ints to null, but I can't seem to get this to work.

    AlterStoredProcedure(
                    name: "[dbo].[FT_People_PersonFullTextSearch]",
                    parametersAction: 
                       p => new { 
                                   searchTerm = p.String(600), 
                                   isArchived = p.Boolean(false), 
                                   isActive = p.Boolean(null), 
                                   genderFilter = p.Int(null), 
                                   rankingFilter = p.Int(null) 
                                 },
                    body: "the body of my stored proc....");

The above code produces

ALTER PROCEDURE [dbo].[FT_People_PersonFullTextSearch]
    @searchTerm [nvarchar](600),
    @isArchived [bit] = 0,
    @isActive [bit],
    @genderFilter [int],
    @rankingFilter [int]
AS
BEGIN

instead of

ALTER PROCEDURE [dbo].[FT_People_PersonFullTextSearch]
    @searchTerm nvarchar(600), 
    @isArchived bit = 0,
    @isActive bit = null,
    @genderFilter int = null,
    @rankingFilter int = null
AS 
BEGIN

Does anyone know how to get the parameters to produce @isActive bit = null?

回答1:

I am using Entity Framework 6.1.1 and I was able to achieve this by doing the following:

AlterStoredProcedure(
    name: "[dbo].[FT_People_PersonFullTextSearch]",
    parametersAction: 
        p => new { 
            searchTerm = p.String(600), 
            isArchived = p.Boolean(false), 
            isActive = p.Boolean(null, "null"), 
            genderFilter = p.Int(null, "null"), 
            rankingFilter = p.Int(null, "null") 
        },
    body: "the body of my stored proc....");

Note that I've just plugged in my solution into your example code, I haven't actually tried running this exact code.

The specific parameter I'm setting there is defaultValueSql: "null".

This gave me a stored procedure which looked a little like this:

ALTER PROCEDURE [dbo].[FT_People_PersonFullTextSearch]
    @searchTerm nvarchar(600), 
    @isArchived bit = 0,
    @isActive bit = null,
    @genderFilter int = null,
    @rankingFilter int = null
AS 
BEGIN