ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 62.8k

How to call stored procedure procedure using entity framework core?

Jul 8 2023 10:22 AM

I work on asp.net core entity framework I have csharp function done using ado dotnet stored proceure 

i need to convert it using entity framwork core

with another meaning how to call stored proceure using entity framework core 6

and i don't need to use ado dotnet stored procedure 

so How to do it please 

   DataTable dtprinters = _IAdcSupportService.GetAvailablePrinters(branchcode);
 if (dtprinters.Rows.Count > 0)
{
}
public interface IAdcSupportService
    {
public DataTable GetAvailablePrinters(string BranchId);
}
public class AdcSupportService:IAdcSupportService
    {
private readonly ADCContext _context;
        public AdcSupportService(ADCContext context)
        {
_context=context;
        }
        public DataTable GetAvailablePrinters(string BranchCode, string DisplayName = null, string PrinterType = null)
        {
            string response = string.Empty;
            SqlCommand cmd = new SqlCommand();
            DataTable dt = new DataTable();
            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = "ADC_GetAvailablePrinters";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 50000;

                cmd.Parameters.AddWithValue("@BranchCode", BranchCode);
                cmd.Parameters.AddWithValue("@DisplayName", DisplayName);
                cmd.Parameters.AddWithValue("@PrinterType", PrinterType);

                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(dt);
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
            return dt;
        }
 ALTER PROCEDURE [dbo].[ADC_GetAvailablePrinters]
	(
		@BranchCode VARCHAR(50),
		@DisplayName VARCHAR(100) = NULL,
		@PrinterType VARCHAR(50) = NULL
	)
AS
BEGIN
	IF @PrinterType IS NOT NULL AND @PrinterType = 'SUB'
	BEGIN
		SELECT PrinterName, PrinterIP, Category, IsActive FROM ZebraSubPrinters WITH (NOLOCK) WHERE DisplayName = @DisplayName AND BranchCode = @BranchCode AND IsActive = 1
	END
	ELSE
	BEGIN
		SELECT DisplayName + ',' + PrinterName as PrinterName,UserPC + ',' + ArabicConfig as UserPC FROM ZebraPrinters WITH (NOLOCK) WHERE  BranchCode = @BranchCode AND Status = 'Y'
	END
END

ADCContext represent context of entity framework that collect all models


Answers (2)