在Ria Services SP1和EF 4.1 POCO中公开关联实体

时间:2011-06-08 23:51:53

标签: c# entity-framework-4.1 wcf-ria-services

我们最近将使用RIA服务的Silverlight 4应用程序和使用EDMX的EF 4.0升级到使用POCO的RIA Services SP1和EF 4.1。升级非常值得,但看起来RIA现在正在以不同方式展示客户关联实体的方式。

例如说我们有以下EF POCO,它们也是我们的RIA实体:

public class Building
{
   public Building()
   {
     Rooms = new List<Room>();
   }

   [Key]
   public int? BuildingID { get; set; }

   public string Name {get; set;}

   [Association("Building_1-*_Rooms", "BuildingID", "BuildingID")]
   [Include]
   [Composition]
   public ICollection<Room> Rooms {get; set;}
}


public class Room
{
   [Key]
   public int? RoomID { get; set; }

   [Required]
   public int? BuildingID { get; set; }

   public string Name {get; set;}

   [Association("Building_1-*_Rooms", "BuildingID", "BuildingID", IsForeignKey= true)]
   [Include]
   public Building Building {get; set;}

   [Association("Room_1-*_Desks", "RoomID", "RoomID")]
   [Include]
   [Composition]
   public ICollection<Desk> Desks { get; set; }
}

public class Desk
{
   [Key]
   public int? DeskID { get; set; }

   [Required]
   public int? RoomID { get; set; }

   public string Name { get; set; }

   [Association("Room_1-*_Desks", "RoomID", "RoomID", IsForeignKey = true)]
   [Include]
   public Room Room { get; set; }
 }

Building是Room的父级,Room是Desk的父级。 Association属性为RIA定义了这些关系。然后,我们使用一个包含所有三个实体的CRUD的简单服务来公开这些实体

public class BuildingDomainService
{

 var _context= new BuildingEFContext(); //Lets just say this is our EF Context that has all three types on it

 public IQueryable<Buildings> GetBuildings()
 {
      return _context.Buildings.Include(x => x.Rooms.Select(y => y.Desks));
 }

 public IQueryable<Rooms> GetRooms()
 {
      return _context.Rooms.Include(x => x.Desks);
 }

 public IQueryable<Desk> GetDesks()
 {
      return _context.Desks;
 }

 //Empty Update and Insert Methods to allow editing on client
 public void UpdateBuilding(Building building){}
 public void InsertBuilding(Building building){}
 public void DeleteBuilding(Building building){}

 public void UpdateRoom(Room room){}
 public void InsertRoom(Room room){}
 public void DeleteRoom(Room room){}

 public void UpdateDesk(Desk desk){}
 public void InsertDesk (Desk desk){}
 public void DeleteDesk (Desk desk){}

}

在客户端上,从BuildingDomainService生成的BuildingDomainContext具有三个公开方法(GetBuildingsQuery(),GetRoomsQuery()和GetDesksQuery()),但只有一个类型为Building的EntitySet,该服务不公开EntitySet适用于房间或书桌。

在我们的客户端应用程序中的某些地方,我们希望维护此对象层次结构,但在其他地方我们可能只想获得它的一部分,例如,如果我们想要查看并编辑房间中的桌子,但不要关心建筑物的房间。由于RIA服务不公开房间或桌面的EntitySet,因此我们无法在不拉动父楼的情况下编辑其中任何一个。

有没有在RIA中维护这些关联,但也允许编辑这个层次结构的一部分,而不必拉入最顶层的父级?

2 个答案:

答案 0 :(得分:3)

我建议您在composition上阅读这篇文章。添加[Composition]属性时,它会影响数据从服务器流向客户端的方式以及可以独立编辑的实体。

答案 1 :(得分:0)

也许使用BuildingDomainContext.EntityContainer.GetEntitySet<Room>()方法会公开您正在寻找的EntitySet。您可以将其放在客户端partial的{​​{1}}实现中。例如:

BuildingDomainContext