Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
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

ADCContext represent context of entity framework that collect all models

What I have tried:

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
Posted

1 solution

I'd strongly suggest to read this: Working with Stored Procedure in Entity Framework Core[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900