Tuesday, May 13, 2025

c# - EF - How to call storedproc with OUTPUT param

You have this stored proc:
ALTER PROCEDURE [dbo].[TEST]
	-- Add the parameters for the stored procedure here
( @Param1 int, @Message varchar(50) output )
AS
BEGIN
  set @Message = 'xxx';

  return '-1';
END
For calling it use this code:
var param1 = new SqlParameter( "Param1", System.Data.SqlDbType.Int );
param1.Direction = System.Data.ParameterDirection.Input;
param1.Value = iYear;

var message = new SqlParameter( "Message", System.Data.SqlDbType.VarChar, 50 ); 
message.Direction = System.Data.ParameterDirection.InputOutput;
message.Value = "";

var ret = context.Database.ExecuteSqlRaw(
  "EXEC TEST @Param1 = @param1, @Message = @message OUTPUT", param1, message
);

MessageBox.Show( message.Value.ToString() );
MessageBox.Show( ret.ToString() );

c# - EF - How to refresh existing DB into EF model

If you have DB and if you want to refresh it`s model into MODEL directory, call this:
dotnet ef dbcontext scaffold "Server=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=C:\_projects\vs\FioTax\FioTax\db.mdf;
Integrated Security=True;Connect Timeout=30" Microsoft.EntityFrameworkCore.SqlServer 
--output-dir Models --context AppDbContext --force