How to use Stored Procedure In Entity Framework

Jun 02, 2019 BY xtreme
Using ADO.NET Model
public IEnumerable<resultModel> GetReport(int? id)
{
	List<resultModel> result = new List<resultModel>();
	using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()))
	{
		using (SqlCommand cmd = new SqlCommand("rpt_getreport", conn))
		{
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
			SqlDataAdapter da = new SqlDataAdapter();
			da.SelectCommand = cmd;

			DataSet ds = new DataSet("report");
			da.Fill(ds);
			result = (ds.Tables[0].AsEnumerable()
				.Select(r => new resultModel
				{                                  
					id = int.Parse(r["Id"].ToString()),
					Date = DateTime.Parse(r["Date"].ToString()),
				})).ToList();
		}
	}
}
public class resultModel
{
	public int id { get; set; }
	public DateTime Date { get; set; }
}
Related