I want get data from one of my models. I am using Entity datamodel where I have some tables in my Entity model. I want to select data from EmployeeTable.
EmployeeTable
----------------
[EmpId]|[Empname]|[EmpAddress]
I want to select [EmpID] and [Empname] columns from table. I don’t know how to do this in Entity Framework and I have to return the data as JSON.
I had tried this methods but I am not geting the data.
How can I write the linq query?
public ActionResult Index()
{
return View();
}
public JsonResult GetData()
{
int Param1;
Param1 = 1;
DataEntitiesModel data = new DataEntitiesModel();
//var procedure=db.Database.SqlQuery<DataEntitiesModel>("ResourceReports @EmployeeID",new SqlParameter("@EmployeeID", Param1) );
//var procedure = db.Database.SqlQuery<DataEntitiesModel>("Select * from EmployeeDetails");
return Json(procedure,JsonRequestBehavior.AllowGet);
}
Here’s the model:
public class DataEntitiesModel
{
public Int16 EmpID{ get; set; }
[Required(ErrorMessage = "Title is required")]
public string EmpName{ get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
[Required(ErrorMessage = "Version is required")]
public string EmpAddress{ get; set; }
}
public class DataEntitiesDBContext : DbContext
{
public DbSet<DataEntitiesModel> ProjectReports { get; set; }
}
Here’s a nice video from Pluralsight to get you started with Entity Framework. You generate a data context from your database and then query this context: