Enabling Navigation Properties from Entity Framework TPH Classes

I have an EF hierarchy that (greatly simplified) looks something like this:

class Room { EntityCollection<Session> Sessions; }
class Session { EntityCollection<Whiteboard> Whiteboards; EntityReference Room; }
class Whiteboard { EntityCollection<WhiteboardShape> WhiteboardShapes; EntityReference Session; }
abstract class WhiteboardShape { EntityReference Whiteboard; }
class WhiteboardShapeEllipse : WhiteboardShape { }
class WhiteboardShapePolyline { WhiteboardShape { EntityCollection<PolylinePoint> PolylinePoints }
class PolylinePoint { EntityReference<WhiteboardShapePolyline> WhiteboardShapePolylineReference; }

In other words, the Room may contain several sessions; each session may contain several boards; and each Whiteboard can contain multiple WhiteboardShapes. These forms can be of various types, including WhiteboardShapePolyline, which itself can contain several PolylinePoints.

When the remote user initially connects to the room, I need to transfer the entire object graph to this user, and I'm trying to figure out how to most efficiently load this graph from the database into memory.

Now, of course, EF allows you to load, for example:

      Room room = ctx.Room
            .Include("Sessions.Whiteboards")
            .FirstOrDefault(r => r.OwnerID == ownerUserID && r.Name == roomName);

But Include () does not allow me to load PolylinePoints. In particular, if I try:

        Room room = ctx.Room
            .Include("Sessions.Whiteboards.WhiteboardShape.PolylinePoint")
            .FirstOrDefault(r => r.OwnerID == ownerUserID && r.Name == roomName);

" Include . EntityType" SlideLinc.Model.WhiteboardShape " " PolylinePoint ".

:

.Include("Sessions.Whiteboards.WhiteboardShapePolyline.PolylinePoint")

:

.Include("Sessions.Whiteboards.WhiteboardShape.WhiteboardShapePolyline.PolylinePoint")

, .

, , , :

        // Make sure we've got everything loaded.
        if (room != null)
        {
            if (!room.Sessions.IsLoaded) { room.Sessions.Load(); }
            foreach (Session session in room.Sessions)
            {
                if (!session.Whiteboards.IsLoaded) { session.Whiteboards.Load(); }
                foreach (Whiteboard whiteboard in session.Whiteboards)
                {
                    if (!whiteboard.WhiteboardShape.IsLoaded) { whiteboard.WhiteboardShape.Load(); }
                    foreach (WhiteboardShape shape in whiteboard.WhiteboardShape)
                    {
                        if (shape is WhiteboardShapePolyline)
                        {
                            WhiteboardShapePolyline polyline = (WhiteboardShapePolyline)shape;
                            if (!polyline.PolylinePoints.IsLoaded) { polyline.PolylinePoints.Load(); }
                        }
                    }
                }
            }
        }

, , , , .

, , , Linq , ; , . , EF .

?

+3
2

, , . , , . (, LINQ), . Include.

+3

( ), , PropertyName Designer.cs , . PropertyName Designer, - , , :

.Include("UnexpectedNameA").Include("UnexpectedNameB")
0

Source: https://habr.com/ru/post/1713199/


All Articles