Ahmed Amen

Ahmed Amen

  • NA
  • 7
  • 3.3k

fill controller from SQL Function in Entity Framework ?

Feb 6 2015 1:35 PM
  1. i have create an sql function in my database that take to Date params and get data from 5 tables.

  2. after that add it to project as entity framework from database and the code generated is:

[DbFunction("Dr_EmploEntities", "SelectEmployee")]         public virtual IQueryable SelectEmployee(Nullable frm_date, Nullable 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("[Dr_EmploEntities].[SelectEmployee](@frm_date, @to_date)", frm_dateParameter, to_dateParameter);         }          public DbSet SelectEmployee_Result { get; set; }  

as you see i have now "SelectEmployee_Result" that don't take any params, and "SelectEmployee" that take two date params.

  1. after that i have create an controller for "SelectEmployee_Result" class.

  2. after that i run my project Index View that working with "SelectEmployee_Result" class give me err:

"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."

  1. and i make breakpoint and see that "SelectEmployee_Result" has no data so i change the Index Code in controller and fill "SelectEmployee" with two date params

  2. and when run got same err msg too.

so how can i fill "SelectEmployee_Result" from 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"?


Answers (2)