Hi
01 - i have create an sql function in my database that take to Date params with code:
CREATE FUNCTION [dbo].[SelectEmployee](@frm_date DATE, @to_date DATE)
RETURNS TABLE
AS
RETURN SELECT
Emplo_ID, Emplo_Name, Work_Date
FROM Employee_Data WHERE Work_Date BETWEEN @frm_date AND @to_date
GO
02 - after that add it to project as entity framework from database and the code generated is:
[DbFunction("Dr_EmploEntities", "SelectEmployee")]
public virtual IQueryable<SelectEmployee_Result> SelectEmployee(Nullable<DateTime> frm_date, Nullable<DateTime> to_date)
{
var frm_dateParameter = frm_date.HasValue ?
new ObjectParameter("frm_date", frm_date) :
new ObjectParameter("frm_date", typeof(DateTime));
var to_dateParameter = to_date.HasValue ?
new ObjectParameter("to_date", to_date) :
new ObjectParameter("to_date", typeof(DateTime));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<SelectEmployee_Result>("[Dr_EmploEntities].[SelectEmployee](@frm_date, @to_date)", frm_dateParameter, to_dateParameter);
}
public DbSet<SelectEmployee_Result> SelectEmployee_Result { get; set; }
03 - after that i have create an controller for SelectEmployee_Result class:
private Dr_EmploEntities db = new Dr_EmploEntities();
// GET: SelectEmployee_Result
public ActionResult Index()
{
return View(db.SelectEmployee_Result.ToList());
}
// GET: SelectEmployee_Result/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SelectEmployee_Result selectEmployee_Result = db.SelectEmployee_Result.Find(id);
if (selectEmployee_Result == null)
{
return HttpNotFound();
}
return View(selectEmployee_Result);
}
04 - after that i run my project and got err in Index View:
The type 'SelectEmployee_Result' is mapped as a complex type. The Set method, DbSet objects, and DbEntityEntry objects can only be used with entity types, not complex types.
05 - and i make breakpoint and see that SelectEmployee_Result has no data so i change the Index Code in controller to be:
public ActionResult Index()
{
var model = db.SelectEmployee(new DateTime(2014, 01, 01), new DateTime(2014, 12, 31));
return View(model);
}
06 - after that index view was working and i can view data , but i got the same err message when in try to open details view:
The type 'SelectEmployee_Result' is mapped as a complex type. The Set method, DbSet objects, and DbEntityEntry objects can only be used with entity types, not complex types.
in this Line:
SelectEmployee_Result selectEmployee_Result = db.SelectEmployee_Result.Find(id);
so how can i fill SelectEmployee_Resultfrom the beginning with data between two dates to let me use it in all views ?
all what i need here is view data i got i edit before saving it in database Like using DataTable but i need to do that from Entity with sql function
and what is difference between "SelectEmployee" that is my function name and that is need two params and SelectEmployee_Result?