Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Improve Entity Framework and Javascript Interaction
    text
    copied!<p>This is a pretty vague/subjective question. I want to know if this is the best way to send/retrieve data to/from the browser using ajax calls. On the back end webservice, I want to use the entity framework. Below are two example functions.</p> <p>The criteria for "best" is speed of writing code, readable code, and robust architecture.</p> <p>Thanks for any feedback and suggestions and comments.</p> <p><strong>Get Function</strong></p> <pre><code>[WebMethod] public AjaxEmployee EmployeeGetById(int employeeID, bool getTimeOff) { using (Time_TrackerEntities ctx = new Time_TrackerEntities()) { var results = from item in ctx.Employees where item.ID == employeeID orderby item.Last_Name select new AjaxEmployee { ID = item.ID, Employee_ID = item.Employee_ID, First_Name = item.First_Name, Middle_Name = item.Middle_Name, Last_Name = item.Last_Name, Supervisor_ID = item.Supervisor_ID, Active = item.Active, Is_Supervisor = item.Is_Supervisor }; var emp = results.FirstOrDefault(); if (getTimeOff) { var results2 = from item2 in ctx.Time_Off where item2.Employee_ID == emp.Employee_ID select new AjaxTime_Off { ID = item2.ID, Employee_ID = item2.Employee_ID, Date_Off = item2.Date_Off, Hours = item2.Hours }; emp.Time_Off = results2.ToList&lt;AjaxTime_Off&gt;(); } return emp; } } </code></pre> <p><strong>Save Function</strong></p> <pre><code>[WebMethod] public bool EmployeeSave(AjaxEmployee emp) { using (Time_TrackerEntities ctx = new Time_TrackerEntities()) { var results = from item in ctx.Employees where item.ID == emp.ID select item; var myEmp = results.FirstOrDefault(); if (myEmp == null) { myEmp = new Employee(); ctx.Employees.AddObject(myEmp); } myEmp.Employee_ID = emp.Employee_ID; myEmp.First_Name = emp.First_Name; myEmp.Middle_Name = emp.Middle_Name; myEmp.Last_Name = emp.Last_Name; myEmp.Supervisor_ID = emp.Supervisor_ID; myEmp.Active = emp.Active; myEmp.Is_Supervisor = emp.Is_Supervisor; return ctx.SaveChanges() &gt; 0; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload