【问题标题】:DbMigration.AlterstoredProcedure (Entity Framework migration): How to represent type smallmoney?DbMigration.AlterstoredProcedure(实体框架迁移):如何表示类型 smallmoney?
【发布时间】:2014-12-29 22:05:23
【问题描述】:

在 Entity Framework 6.1 中,在基于 C# 代码的迁移(使用 System.Data.Entity.Migrations.DbMigration)中,当使用 DbMigration.AlterStoredProcedure 方法更改存储过程定义时,添加或修改smallmoney 类型的存储过程参数(在 SQL Server 2012 上)?

例如,如果我有一个修改现有 SQL Server 存储过程的迁移方法,该存储过程采用三个参数,类型分别为 intvarcharsmallmoney

public partial class MyCustomMigration : DbMigration
{
    public override void Up()
    {
        this.AlterStoredProcedure("dbo.EditItem", c => new
        {
            ItemID = c.Int(),
            ItemName = c.String(),
            ItemCost = /* What goes here to represent the smallmoney SQL Server type? */
        },
        @" (New sproc body SQL goes here) ");
    }

    // ...
}

【问题讨论】:

  • 您可以尝试使用ItemCost = c.Decimal(storeType: "smallmoney")... 实际上您可以在这里使用任何方法,例如c.Int()c.Double() 或任何其他内容,直到您明确指定 storeType: "smallmoney"
  • 如何为字符串变量指定长度的相关问题:stackoverflow.com/questions/7341783/…

标签: c# sql-server entity-framework entity-framework-6.1


【解决方案1】:

感谢 nemesv,感谢您在评论中提供的提示!我缺少的是在设置存储过程参数时指定的类型,即“Int”和“String”在:

c => new
    {
        ItemID = c.Int(),
        ItemName = c.String(),
        //...
    }

...实际上是方法,这些方法中的每一个(在 System.Data.Entity.Migrations.Builders.ParameterBuilder 类上)都有一组可选参数,这些参数会影响从迁移脚本生成的 SQL。

smallmoney-type 存储过程参数的情况下,我最终使用了:

    ItemCost = c.Decimal(precision: 10, scale: 4, storeType: "smallmoney")

precision: 10 和 scale: 4 的值来自 MSDN 文章 money and smallmoney (Transact-SQL),其中指定 smallmoney 的精度(总位数)为 10 和小数位数(小数点右侧的位数) 4 点)(对于 SQL Server 2008 及更高版本)。

所以我的完整迁移代码是:

public override void Up()
{
    this.AlterStoredProcedure("dbo.EditItem", c => new
    {
        ItemID = c.Int(),
        ItemName = c.String(),
        ItemCost = c.Decimal(precision: 10, scale: 4, storeType: "smallmoney")
    },
    @" (New sproc body SQL goes here) ");
}

产生了 SQL:

ALTER PROCEDURE [dbo].[EditItem]
    @ItemID [int],
    @ItemName [nvarchar](max),
    @ItemCost [smallmoney]
AS
BEGIN
    (New sproc body SQL goes here)
END

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多