RIA服务:实体框架参考实体 [英] RIA Services: Entity Framework Reference Entities

查看:131
本文介绍了RIA服务:实体框架参考实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用具有实体框架后端的RIA服务。该模型由设计师自动生成。 (VS 2010,.NET 4.0,EF 4.0,SilverLight 4.0)
我正在运行的问题是参考属性正如预期在服务层加载,但是当该信息被传递到SilverLight应用程序参考实体变为NULL。

I'm attempting to use RIA services with a Entity Framework back end. The Model was auto generated by the designer. (VS 2010, .NET 4.0, EF 4.0, SilverLight 4.0) The issue that I'm running into is that Reference Properties are being loaded as expected at the Service Layer, but when that information is passed over to the SilverLight App the Reference Entity becomes NULL.

我已经使用两种方法来填充参考实体:

I've used two approaches to populating the Reference Entity:

public Employee GetEmployeeByID(int employeeID)
  {
   var result = this.ObjectContext.Employees
    .Include("EmployeeRoles")
    .Where(emp => emp.EmployeeID == employeeID)
    .FirstOrDefault();
   return result;
  }

And

public Employee GetEmployeeByID(int employeeID)
  {
   var result = this.ObjectContext.Employees
    .Where(emp => emp.EmployeeID == employeeID)
    .FirstOrDefault();

   if (result != null && result.EmployeeRoleReference.IsLoaded == false)
   {
    result.EmployeeRoleReference.Load();
   }
   return result;
  }

两种方法看起来正确地填充了Reference对象,返回。
但是,当我尝试通过RIA服务从UI引用Employee对象时,EmployeeRole引用的实体为NULL。

Both methods appear to correctly populate the Reference objects when examining the objects prior to the return. However when I attempt to reference the Employee Object from the UI via RIA Services, the EmployeeRole referenced entity is NULL.

在前端我打电话:

public void LoadEmployeeProfile()
  {
   int empID = WebContext.Current.User.EmployeeID;
   LoadOperation<Employee> loadEmployee = _appcontext.Load(_appContext.GetEmployeeByIDQuery(empID));
   loadEmployee.Complete += new System.EventHandler(loadEmployee_Completed);
  }

  void LoadEmployee_Completed(object sender, System.EventArgs e)
  {
   LoadOperation<Employee> loadEmployee = sender as LoadOperation<Employee>;
   if (loadEmployee == null)
    return;

   loadEmployee.Completed -= LoadEmployee_Completed;
   foreach (Employee employee in loadEmployee.Entities)
   {
    this.EmployeeProfile == employee;
    break;
   }
  }

这似乎是所有教科书类型的东西,但我没有能够追赶一下,为什么RIA服务没有像我所预期的那样填充参考实体。

This appears to be all textbook type stuff, but I haven't been able to chase down a lead as to why the RIA services isn't populating the Reference entity as I would have expected it too.

推荐答案

要澄清,您正在尝试请求员工记录,并包括数据库/实体模型中的引用链接定义的EmployeeRoles。

To clarify, you are trying to request an Employee record and include EmployeeRoles as defined by referential links in the database/Entity Model.

Web RIA项目包含Employee的元数据,我将检查您是否在Employees元数据中使用[Include]属性标记了EmployeeRoles。

In the .Web RIA project that contains the metadata for Employee, I would check that you have marked EmployeeRoles in your Employee metadata with the [Include] attribute.

您需要包含引用在您的查询和模型元数据中。

You need to include the references in your query and in the model metadata.

希望这有帮助。

这篇关于RIA服务:实体框架参考实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆