(Please note: I've completely changed my answer based upon clarification in the comments - Please see history for original answer)
After some discussion in the comments for this question, and subsequent clarification, it sounds to me like, since you are creating your own constructor, you are not initializing the Orderlines property of the Order object.
Note that in the default (parameterless) constructor for Order, there should be a line of code that initializes the Orderlines EntitySet property. Something like:
public Order()
{
this._Orderlines = new EntitySet<Orderlines>(new Action<Orderline>(this.attach_Orderline), new Action<Orderline>(this.detach_Orderline));
OnCreated();
}
Your new constructor, when called, does not appear to be creating the internal EntitySet<T>
object that is exposed via the .Orderlines
property. This seems to explain the NullReferenceException
that your experiencing, as your own constructor never calls the default constructor, nor does it replicate the initialization of the Orderlines EntitySet<T>
object/property.
The best way to achieve this is to "chain" your constructors such that your overloaded constructor that takes the Reservation
object as a parameter first calls the default (parameterless) constructor such that this initialization can take place. This would be achieved using something like:
public Order(Reservation reservation) : this()
{
this.date = DateTime.Now;
this.Reserveration = reservation;
OrderLine orderLine = new OrderLine(reservation);
//this.attach_Orderlines(orderLine);
this.Orderlines.Add(orderLine);
}
Your this.Orderlines.Add(orderLine)
line of code should now work correctly (assuming the Orderline orderline = new OrderLine(reservation)
line of code does not return null!)