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
Mayooran NavamanyPosted Jul 9, 2023, 9:53 AM
Hi ahmed salah
How to call stored procedure using entity framework core 5, Read the articles, I think it's helpful for you.
https://www.codemag.com/Article/2101031/Calling-Stored-Procedures-with-the-Entity-Framework-in-.NET-5.
https://www.entityframeworktutorial.net/efcore/working-with-stored-procedure-in-ef-core.aspx.
Prasad RaveendranPosted Jul 8, 2023, 2:10 PM
To invoke a stored procedure using Entity Framework 6 in C#, you can follow these steps:
1. Create a DbContext class:
Replace "YourDbContext" with the name of your DbContext class, and "YourConnectionString" with the actual connection string for your database.
2. Define a method in your DbContext class to execute the stored procedure:
This method accepts the stored procedure name and an array of
SqlParameterobjects as parameters. It constructs the command text and uses theExecuteSqlCommandmethod to execute the stored procedure.3. Call the method to execute the stored procedure:
Replace "YourDbContext" with the actual name of your DbContext class, "YourStoredProcedureName" with the name of your stored procedure, and
value1,value2, etc., with the actual values for your parameters.Make sure you have the necessary
usingstatements at the top of your file:That's it! You can now invoke a stored procedure using Entity Framework 6 in C#.