在Ria Services SP1和EF 4.1 POCO中披露关联实体

本文关键字:POCO 实体 关联 Services Ria SP1 EF | 更新日期: 2023-09-27 17:58:39

我们最近将使用RIA Services和EF 4.0以及EDMX的Silverlight 4应用程序升级为使用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(。

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

是否有办法在RIA中维护这些关联,同时允许编辑该层次结构的一部分,而不必拉入最顶端的父级?

在Ria Services SP1和EF 4.1 POCO中披露关联实体

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

也许使用BuildingDomainContext.EntityContainer.GetEntitySet<Room>()方法会暴露您要查找的EntitySet。您可以将其放在客户端BuildingDomainContextpartial实现中。例如:

public partial class BuildingDomainContext
{
    public EntitySet<Room> Rooms
    {
        get
        {
            return EntityContainer.GetEntitySet<Room>();
        }
    }
}