DbMigration.AlterstoredProcedure (Entity platform migration): how to represent a smallmoney type?

In Entity Framework 6.1, in code-based porting in C # (using System.Data.Entity.Migrations.DbMigration) when changing the definition of a stored procedure using DbMigration.AlterStoredProcedure , what is the correct syntax for adding or changing a parameter of a stored procedure of type smallmoney (to SQL Server 2012)?

For example, if I have a migration method that modifies an existing SQL Server stored procedure that 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) "); } // ... } 
+2
c # sql-server entity-framework
Dec 29 '15 at 22:05
source share
1 answer

Thanks nemesv for the tip in your comment! What I was missing was that the types are specified when setting the parameters of the stored procedure, that is, "Int" and "String" in:

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

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

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

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

Accuracy values: 10 and scale: 4 from the MSDN article money and smallmoney (Transact-SQL) , which indicates that smallmoney has an accuracy (total number of digits) of 10 and a scale (# digits to the right of the decimal point) 4 (for SQL Server 2008 and above).

So my full transition code:

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

What caused SQL:

 ALTER PROCEDURE [dbo].[EditItem] @ItemID [int], @ItemName [nvarchar](max), @ItemCost [smallmoney] AS BEGIN (New sproc body SQL goes here) END 
+2
Dec 30 '15 at 19:30
source share



All Articles