I am encountering a conceptual hurdle with how Entity Framework is inferring entity relationships. So while I have solved my problem, it doesn't make sense to me why it works.
I have the following entities, here in simplified form, from the Geometry Class Library.
Line class, with primary/foreign key properties hidden for brevity and focus on the question:
public class Line
{
public virtual Point BasePoint
{
get { return _basePoint; }
set { _basePoint = value; }
}
private Point _basePoint;
public virtual Direction Direction
{
get { return _direction; }
set { _direction = value; }
}
private Direction _direction;
}
Vector class, a child of Line, also with primary/foreign key properties hidden:
public class Vector : Line
{
public virtual Distance Magnitude
{
get { return _magnitude; }
set { _magnitude = value; }
}
private Distance _magnitude;
public virtual Point EndPoint
{
get { return new Point(XComponent, YComponent, ZComponent) + BasePoint; }
}
}
LineSegment class, a child of Vector, also with primary/foreign key properties hidden:
public partial class LineSegment : Vector
{
public virtual Distance Length
{
get { return base.Magnitude; }
set { base.Magnitude = value; }
}
public List<Point> EndPoints
{
get { return new List<Point>() { BasePoint, EndPoint }; }
}
}
It is my understanding that Entity Framework ignores getter-only properties, only mapping properties with both a getter and a setter. However, to avoid getting an error of
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
on insert of a LineSegment into the database (Line and Vector work fine), I have to have the following in my model creation:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...
// Set up Line model
modelBuilder.Entity<Line>()
.HasKey(line => line.DatabaseId);
modelBuilder.Entity<Line>()
.HasRequired(line => line.BasePoint)
.WithMany()
.HasForeignKey(line => line.BasePoint_DatabaseId); // Identify foreign key field
modelBuilder.Entity<Line>()
.HasRequired(line => line.Direction)
.WithMany()
.HasForeignKey(line => line.Direction_DatabaseId); // Identify foreign key field
modelBuilder.Entity<Vector>()
.HasRequired(vector => vector.Magnitude)
.WithMany()
.HasForeignKey(vector => vector.Magnitude_DatabaseId); // Identify foreign key field
modelBuilder.Entity<LineSegment>()
.Ignore(lineSegment => lineSegment.Length);
modelBuilder.Entity<LineSegment>() // Why this? EndPoints is a getter only property
.Ignore(lineSegment => lineSegment.EndPoints);
}
Most of that makes sense to me, but for Entity Framework to understand my model and not produce the error quoted above, why must I include that last statement?
It seems that Entity Framework automatically ignores getter-only properties of the type string, primitive types, and enumeration types. In all other cases you have to ignore them explicitly, using .Ignore()
method or [NotMapped]
annotation.