实体框架——使用默认参数调用存储过程

时间:2021-09-03 21:48:13

I have some stored procedures mapped in Entity Framework using Database First. It creates strongly typed methods that you can call to run the stored procedures. I've run into a significant problem, however, in that I don't see any way to call these methods it created with the default parameters defined in the stored procedures. This means:

我首先使用数据库在Entity Framework中映射了一些存储过程。它创建强类型的方法,您可以调用它来运行存储过程。然而,我遇到了一个重要的问题,因为我看不到任何方法来调用它使用存储过程中定义的默认参数创建的这些方法。这意味着:

a) I have to manually add the default parameters to the method calls, which is brittle, if the default parameter value were to ever change.

a)如果要更改默认参数值,我必须手动向方法调用添加默认参数,这很脆弱。

b) Write method overloads by hand. This basically eliminates the benefit of generating a model from the database in the first place.

b)手写方法重载。这基本上消除了从数据库生成模型的好处。

Does anyone know if there is a better solution to this problem?

有人知道这个问题是否有更好的解决办法吗?

Thanks.

谢谢。

2 个解决方案

#1


5  

As of Jan 2013, there's no supported way to do have the Entity Framework do this.

从2013年1月开始,没有支持实体框架这样做的方法。

I have opened a feature request here.

我在这里打开了一个特性请求。

#2


3  

Here's a hacky workaround. I don't know if there's a feature in EF to support optional parameters, but you can try to emulate it - if you're willing to change the stored procedures.

这里有一个出租汽车司机解决方案。我不知道EF中是否有支持可选参数的特性,但是您可以尝试模拟它——如果您愿意更改存储过程的话。

You can change the default value in the definition of the stored procedure to NULL, and then in the body of the procedure, replace NULL parameters with the desired default value. Then from the code using EF you could pass null to indicate using the default. This way at least the default is only defined in the SP itself.

您可以将存储过程定义中的默认值更改为NULL,然后在过程体中,将NULL参数替换为所需的默认值。然后从使用EF的代码中,您可以传递null来表示使用默认值。这样,至少默认值只在SP本身中定义。

Drawbacks include of course that now NULL becomes an "out-of-band" value, meaning you can't actually pass it to the SP if that's what you need (besides having to change your SPs and ensuring all future ones use this weird convention)

缺点包括,现在NULL变成了一个“带外”值,这意味着如果你需要的话,你不能将它传递给SP(除了必须改变你的SP并确保所有未来的SP使用这个奇怪的约定)

#1


5  

As of Jan 2013, there's no supported way to do have the Entity Framework do this.

从2013年1月开始,没有支持实体框架这样做的方法。

I have opened a feature request here.

我在这里打开了一个特性请求。

#2


3  

Here's a hacky workaround. I don't know if there's a feature in EF to support optional parameters, but you can try to emulate it - if you're willing to change the stored procedures.

这里有一个出租汽车司机解决方案。我不知道EF中是否有支持可选参数的特性,但是您可以尝试模拟它——如果您愿意更改存储过程的话。

You can change the default value in the definition of the stored procedure to NULL, and then in the body of the procedure, replace NULL parameters with the desired default value. Then from the code using EF you could pass null to indicate using the default. This way at least the default is only defined in the SP itself.

您可以将存储过程定义中的默认值更改为NULL,然后在过程体中,将NULL参数替换为所需的默认值。然后从使用EF的代码中,您可以传递null来表示使用默认值。这样,至少默认值只在SP本身中定义。

Drawbacks include of course that now NULL becomes an "out-of-band" value, meaning you can't actually pass it to the SP if that's what you need (besides having to change your SPs and ensuring all future ones use this weird convention)

缺点包括,现在NULL变成了一个“带外”值,这意味着如果你需要的话,你不能将它传递给SP(除了必须改变你的SP并确保所有未来的SP使用这个奇怪的约定)