DbMigration.AlterstoredProcedure(实体框架迁移):如何表示类型smallmoney? [英] DbMigration.AlterstoredProcedure (Entity Framework migration): How to represent type smallmoney?

查看:159
本文介绍了DbMigration.AlterstoredProcedure(实体框架迁移):如何表示类型smallmoney?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架6.1中,在基于C#代码的迁移(使用System.Data.Entity.Migrations.DbMigration)中,当使用 DbMigration.AlterStoredProcedure 方法,添加或修改$的存储过程参数的正确语法是什么? c $ c> smallmoney 类型(在SQL Server 2012上)?



例如,如果我有一个修改现有SQL Server的迁移方法存储过程需要三个参数,类型为 int varchar smallmoney

  public partial class MyCustomMigration:DbMigration 
{
public override void Up )
{
this.AlterStoredProcedure(dbo.EditItem,c => new
{
ItemID = c.Int(),
ItemName = c。 String(),
ItemCost = / *什么去es这里代表smallmoney SQL Server的类型? * /
},
@(新的sproc body SQL到这里));
}

// ...
}


解决方案

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

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

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



对于 smallmoney 类型存储过程参数,最后使用:

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

精度:10和比例:4的值从MSDN文章 money和s​​mallmoney(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)
},
@(新的sproc body SQL到这里));
}

哪个生成SQL:



pre> ALTER PROCEDURE [dbo]。[EditItem]
@ItemID [int],
@ItemName [nvarchar](max),
@ ItemCost [smallmoney]
AS
BEGIN
(新的sproc body SQL在这里)
END


In Entity Framework 6.1, in a C# code-based migration (using System.Data.Entity.Migrations.DbMigration), when altering a stored procedure definition using the DbMigration.AlterStoredProcedure method, what's the proper syntax for adding or modifying a stored procedure parameter of the smallmoney type (on SQL Server 2012)?

For example, if I have a migration method that modifies an existing SQL Server stored procedure which takes three parameters, of type int, varchar, and smallmoney respectively:

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

    // ...
}

解决方案

Thanks, nemesv, for the hint in your comment! What I was missing is that the types being specified while setting up the stored procedure parameters, i.e. "Int" and "String" in:

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

...are actually methods, and each of those methods -- on class System.Data.Entity.Migrations.Builders.ParameterBuilder -- has a set of optional parameters which affect the SQL generated from the migration script.

In the case of a smallmoney-type stored procedure argument, I ended up using:

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

The values of precision: 10 and scale: 4 are from the MSDN article money and smallmoney (Transact-SQL), which specifies that smallmoney has precision (total # of digits) of 10 and scale (# of digits to the right of the decimal point) of 4 (for SQL Server 2008 and higher).

So my complete migration code was:

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

Which produced the SQL:

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

这篇关于DbMigration.AlterstoredProcedure(实体框架迁移):如何表示类型smallmoney?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆