How to call stored procedure procedure using entity framework core?

ahmedaziz

Well-known member
Joined
Feb 22, 2023
Messages
55
Programming Experience
1-3
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

How to call stored procedure using entity framework core without sing ado.net:
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
 
Manually, using raw sql off of context directly

If you use EF core power tools VS extension it can generate the code to call your proc
 
I must have misunderstood what the OP was asking for. I thought that he wanted to get rid of calling the stored procedure, and replace it with straight EF code instead.
 
Back
Top Bottom