我们最近将使用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; }
}
建筑物是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,该服务不会为Room或暴露EntitySet。台。
在客户端应用程序中的某些地方,我们希望维护该对象的层次结构,而在其他地方,我们可能只希望得到它的一部分,例如,如果我们要查看并编辑一个房间中的办公桌,但不在乎关于建筑物中的房间。由于RIA Services不会公开Room或Desk的EntitySet,因此我们在不拉出父建筑物的情况下也无法编辑其中任何一个。
无论如何,是否有必要在RIA中维护这些关联,但还允许编辑此层次结构的一部分,而不必引入最顶层的父级?
最佳答案
我建议阅读composition上的这篇文章。添加[Composition]属性时,它会影响数据从服务器流向客户端的方式以及可以独立编辑的实体。