0
votes

I feel like I running into brick walls over and over with this WCF RIA Service.

I have a WCF RIA Service method that returns the employee based on the ID parameter you pass to it. It is working with ajax calls to the URI using:

/WCF_RIA_ServiceData.svc/GetEmployeeByID?EmployeeID=1

I want to use this same query in an MVC controller so I have tried this:

// GET: /Employee/Details/5
public ActionResult Details(int id)
{
    WCF_RIA_ServiceData y = new WCF_RIA_ServiceData();
    WCF_RIA_ServiceDataService z = new WCF_RIA_ServiceDataService();

    WCF_RIA_Project.Employee employee = (WCF_RIA_Project.Employee)y.GetEmployeeByID(id);
    WCF_RIA_Project.Employee employee2 = (WCF_RIA_Project.Employee)z.GetEmployeeByID(id);

    ViewData["EmployeeID"] = id;
    ViewData["EmployeeFirstName"] = employee.FirstName;
    ViewData["EmployeeLastName"] = employee.LastName;
    return View();
}

Here is the method in the WCF RIA Service:

[Query(HasSideEffects = true)]
    public IQueryable<WCF_RIA_Project.Employee> GetEmployeeByID(int? EmployeeID)
    {
        var empData = from Employee in this.Context.Employees
                      where Employee.Id == EmployeeID
                      select new WCF_RIA_Project.Employee
                      {
                           ID = Employee.Id,
                           FirstName = Employee.FirstName,
                           LastName = Employee.LastName
                      };
        return empData;
    } 

I have tried so many ways to get this to return the correct data, but I just get null every time. Is there something that I am missing?

I have seen other examples of this that use the database context, but I can't find any way of letting my MVC application see it. Any help is greatly appreciated!

Edit: Because this project started off as a Lightswitch application, I have the Employees table exposed via oData, and querying the oData service results in the exact same thing (null).

ApplicationData appdata = new ApplicationData();
Employee employee = appdata.Employees_Single(id);
1

1 Answers

0
votes

I was able to solve this issue, it seems that my main problem was being in the wrong namespace. The default one is the Lightswitch Implementation which (I think?) is automatically generated.

Here is all of the working code if it interests anyone:

Controller Method:

 public ActionResult Details(int id)
    {
        WCF_RIA_Project.WCF_RIA_Service wcfria = new WCF_RIA_Project.WCF_RIA_Service();
        WCF_RIA_Project.Employee employee = wcfria.GetEmployeeByID(id);

        ViewData["EmployeeID"] = id;
        ViewData["EmployeeFirstName"] = employee.FirstName;
        ViewData["EmployeeLastName"] = employee.LastName;
        return View();
    }

WCF RIA Service Method:

[Query(IsComposable=false)]
    public Employee GetEmployeeByID(int? EmployeeID)
    {
        var empData = from Employee in this.Context.Employees
                      where Employee.Id == EmployeeID
                      select new Employee
                      {
                          ID = Employee.Id,
                          FirstName = Employee.FirstName,
                          LastName = Employee.LastName
                      };
        return empData.Single();
    }