Entity Framework 6 High Download List

Entity Framework loads everything in Polygon, but my list LineSegments. What am I missing?

Polygon:

public class Polygon
{
    List<LineSegment> _lineSegments = new List<LineSegment>();
    public List<LineSegment> LineSegments
    {
        get { return _lineSegments; }
        protected set { _lineSegments = value; }
    }

    public Point BasePoint { get; protected set; }

    public Vector NormalVector { get; protected set; }

    public int? DatabaseId { get; set; }

    // ...
}

Class LineSegment ( Polygonhas a list of them)

public class LineSegment
{
    public virtual Distance Magnitude
    {
        get { return _magnitude; }
        set { _magnitude = value; }
    }
    private Distance _magnitude;

    public virtual Point BasePoint
    {
        get { return _basePoint; }
        set { this._basePoint = value; }
    }
    private Point _basePoint;

    public virtual Direction Direction
    {
        get { return _direction; }
        set { _direction = value; }
    }
    private Direction _direction;

    public int? DatabaseId { get; set; }

    // ...
}

And here is the relationship setting in the model:

modelBuilder.Entity<Polygon>()
        .HasMany(polygon => polygon.LineSegments);

So, there is a table for both Polygons and LineSegments, and they are inserted correctly where LineSegment has a link to Polygon. But when I try to restore them using the downloaded download, it does not load the list. I have LineSegment properties listed in include, but it does not work. I think I need to change the relationship setting in the model, but I don’t know how to do it. How can I fix this so that I can eagerly load the LineSegments list when Polygon boots up? Here's the request:

    private static List<Expression<Func<Polygon, object>>> polygonRegionNaviationProperties = new List<Expression<Func<Polygon, object>>>()
    {
        (polygon => polygon.BasePoint),
        (polygon => polygon.BasePoint.X),
        (polygon => polygon.BasePoint.Y),
        (polygon => polygon.BasePoint.Z),
        (polygon => polygon.NormalVector),
        (polygon => polygon.NormalVector.Direction),
        (polygon => polygon.NormalVector.Direction.Phi),
        (polygon => polygon.NormalVector.Direction.Theta),
        (polygon => polygon.NormalVector.Magnitude),
        (polygon => polygon.NormalVector.BasePoint.X),
        (polygon => polygon.NormalVector.BasePoint.Y),
        (polygon => polygon.NormalVector.BasePoint.Z),
        (polygon => polygon.LineSegments),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.Direction)),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.Direction.Phi)),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.Direction.Theta)),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.Magnitude)),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.BasePoint.X)),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.BasePoint.Y)),
        (polygon => polygon.LineSegments.Select(lineSegment => lineSegment.BasePoint.Z))
    };

    public Polygon GetPolygon(int? databaseId)
    {
        if(databaseId != null)
        {
            Polygon retrievedPolygon = Query((polygon => polygon.DatabaseId == databaseId), polygonRegionNaviationProperties);
            return retrievedPolygon;
        }
        else
        {
            return null;
        }
    }

    public override Polygon Query(Expression<Func<Polygon, bool>> match, List<Expression<Func<Polygon, object>>> includes = null)
    {
        using (var databaseContext = new ClearspanDatabaseContext())
        {
            databaseContext.Database.Log = Console.Write;

            if (includes != null)
            {
                var dataSet = databaseContext.Set<Polygon>(); // Get the relevant DataSet
                Polygon retrievedObject = includes.Aggregate( // Eagerly load the passed navigation properties
                        dataSet.AsQueryable(),
                        (current, include) => current.Include(include)
                    ).SingleOrDefault(match);
                databaseContext.Entry(retrievedObject).Collection(polygon => polygon.LineSegments).Load();
                return retrievedObject;
            }
            else
            {
                Polygon retrievedObject = databaseContext.Set<Polygon>().SingleOrDefault(match);
                databaseContext.Entry(retrievedObject).Collection(polygon => polygon.LineSegments).Load();
                return retrievedObject;
            }
        }
    }

UPDATE

, .

  • .
  • (git submodule init git submodule update ) ( , SourceTree)
  • PostgreSQL script, , . .
  • , , InsertAndRetrievePolygon. , List , , .

, , . , , GeometryClassLibrary. ,

+4
3

Polygon.LineSegments:

public virtual List<LineSegment> LineSegments
{
    get { return this._Edges.Select(e => (LineSegment)e).ToList(); }
    set { _Edges = value.ToList<IEdge>(); }
}

EF , , , . : EF ? _Edges, , ToList() , LineSegments . _Edges .

, Include() -ing Edges LineSegments, Edges:

protected List<IEdge> _Edges = new List<IEdge>();
public virtual List<IEdge> Edges
{
    get { return _Edges; }
    set { _Edges = value; }
}

EF . , , . LineSegments, , EF. , , (- , ) .

, . XyzEntityFramework (, LineSegmentEntityFramework), , , EF. , .

+3

:

public class Polygon
{
    public virtual List<LineSegment> LineSegments
    {
        get;
        set;
    }


    // ...
}

. , . :

var dataSet = databaseContext.Set<Polygon>();
Polygon retrievedObject = dataSet.Include(i => i.LineSegments).SingleOrDefault(match);

return retrievedObject;

?

+1

" " Code First . http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

, . LineSegments . . , Polygon LineSegments .

public class Polygon 
{
    public Polygon()
    {
        LineSegments = new List<LineSegment>();
    }

    public virtual List<LineSegment> LineSegments
    {
        get;
        set;
    }


    // ...
}

public class LineSegment
{
   public virtual Polygon Polygon { get; set; }
}
0

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


All Articles