gpt4 book ai didi

c# - 更改数据库迁移中的存储过程 EF 6 Code First - 如何将 null 作为参数的默认值传递

转载 作者:太空狗 更新时间:2023-10-30 01:19:18 30 4
gpt4 key购买 nike

我正在使用空迁移来更新数据库中的存储过程。存储过程是在初始创建数据库时添加的自定义存储过程。

我在 DbMigration 类中发现了“AlterStoredProcedure”方法,它可以更新存储过程,但是我必须传递存储过程的参数,我想设置 bool 值和一些整数的默认值为 null,但我似乎无法让它工作。

    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....");

以上代码产生

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

代替

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

有谁知道如何获取参数来产生@isActive bit = null

最佳答案

我使用的是 Entity Framework 6.1.1,我能够通过执行以下操作实现这一点:

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....");

请注意,我只是将我的解决方案插入到您的示例代码中,我实际上并没有尝试运行这个确切的代码。

我在那里设置的具体参数是defaultValueSql: "null"

这给了我一个看起来有点像这样的存储过程:

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

关于c# - 更改数据库迁移中的存储过程 EF 6 Code First - 如何将 null 作为参数的默认值传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23517164/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com