title: Why reference properties works only through context
body: <p>I have two classes Order and OrderDetail:</p>
  
  <pre><code>  public class Order : Entity
  {
      public Order(KitchenAppContext context) : base(context)
      {
  
      }
      public Order() : base()
      {
  
      }
      public DateTime Date { get; set; }
      public Guid MenuId { get; set; }
      public virtual Menu Menu { get; set; }
      public bool IsClosed { get; set; }
      public decimal Price { get; set; }
      public virtual int PeopleCount { get { return Details.Count; } }
      public virtual List&lt;OrderDetail&gt; Details { get; set; } = new List&lt;OrderDetail&gt;();
  }
  
   public class OrderDetail : Entity
  {
      public OrderDetail(KitchenAppContext context) : base(context)
      {
  
      }
  
      public OrderDetail() : base()
      {
  
      }
      public Guid UserId { get; set; }
      public virtual User User { get; set; }
      public virtual List&lt;PaymentDetail&gt; Payments { get; set; } = new List&lt;PaymentDetail&gt;();
      public virtual Order Order { get; set; }
      public Guid OrderId { get; set; }
  }
  </code></pre>
  
  <p>They are mapped like this:</p>
  
  <pre><code>  void OrderMapping(ModelBuilder builder)
      {
          var etBuilder = builder.Entity&lt;Order&gt;();
          etBuilder.HasKey(m =&gt; new { m.Id });
          etBuilder.HasOne(o =&gt; o.Menu).WithMany(a =&gt; a.Orders).HasForeignKey(f =&gt; f.MenuId);
          etBuilder.HasMany(o =&gt; o.Details).WithOne(d =&gt; d.Order).HasForeignKey(f =&gt; f.OrderId);
      }
  
    void OrderDetailMapping(ModelBuilder builder)
      {
          var etBuilder = builder.Entity&lt;OrderDetail&gt;();
          etBuilder.HasKey(m =&gt; new { m.Id });
          etBuilder.HasOne(o =&gt; o.User).WithMany(u =&gt; u.Details).HasForeignKey(f =&gt; f.UserId);
          etBuilder.HasOne(o =&gt; o.Order).WithMany(u =&gt; u.Details).HasForeignKey(f =&gt; f.OrderId);
          etBuilder.HasMany(o =&gt; o.Payments).WithOne(d =&gt; d.OrderDetail).HasForeignKey(f =&gt; f.OrderDetailId);
      }
  </code></pre>
  
  <p>When I create order and order details :</p>
  
  <pre><code>  var order = new Order(Context);
          Context.Orders.Add(order);
          var oderDetail = new OrderDetail(Context) { Order = order };
  </code></pre>
  
  <p>Details of order empty there and OrderId of orderdetails is null also. When I add created order detail in context then it will be added to Details and OrderId becomes Id of created order. Why it works only when I add it to context? I want that it works without adding it to context. Maybe, I should do something in consctructor of classes (with Context parameter)? How can I do this?</p>
  
  <p><strong>EDIT:</strong>
  Order and OrderDetails classes inherited from abstact class Entity:</p>
  
  <pre><code> public abstract class Entity
  {
      Guid id;
      public Guid Id
      {
          get
          {
              if (id == null || id == Guid.Empty)
              {
                  id = Guid.NewGuid();
              }
              return id;
          }
          set
          {
              id = value;
          }
      }
  
      public Entity(KitchenAppContext context)
      {
          Context = context;
      }
  
      public Entity()
      {
  
      }
      public MainContext Context;
  }
  </code></pre>
  
  <p>Also, as you see, I have constructor without parameter. I created them because EF shows this message when I'm getting entities from context: </p>
  
  <pre><code>System.InvalidOperationException: 'A parameterless constructor was not found on entity type 'Order'. In order to create an instance of 'Order' EF requires that a parameterless constructor be declared.'
  </code></pre>
  
  <p>How can I avoid this error without creating conscrtuctors without parameter?</p>