title: The EntityKey property can only be set when the current value of the property is null with n-tier web applicaiton
body: <p>I am working with n-tier web application with Entity Framework with ObjectDataSource.
  In my web application there are Complains &amp; WorkOrder entities which has many to one or zero relationship.</p>
  
  <p>When a WorkOrder is added user can select for which complain it is for. This is what i tried to do.</p>
  
  <pre><code>&lt;asp:DetailsView ID="DVComplain" runat="server" 
          DefaultMode="Insert" AutoGenerateInsertButton="True" 
          AutoGenerateRows="False" DataSourceID="ODSAddWorkOrder" 
          oniteminserting="DetailsView1_ItemInserting" &gt;
          &lt;Fields&gt;
              &lt;asp:TemplateField HeaderText="Complain"&gt;
                 &lt;InsertItemTemplate&gt;
                     &lt;asp:DropDownList ID="DDLComplain" runat="server" DataTextField="ComplainID" 
                         DataValueField="ComplainID" DataSourceID="EDSComplains" 
                         oninit="DDLComplain_Init"&gt;
                     &lt;/asp:DropDownList&gt;
                     &lt;asp:CheckBox ID="CBIsWOOfComplain" runat="server" 
                         oninit="CBIsWOOfComplain_Init" /&gt;
                     &lt;asp:EntityDataSource ID="EDSComplains" runat="server" 
                         ConnectionString="name=MMEntities" DefaultContainerName="MMEntities" 
                         EnableFlattening="False" EntitySetName="ComplainMasters" 
                         EntityTypeFilter="ComplainMaster" Select="it.[ComplainID]" OrderBy="it.ComplainID"&gt;
                     &lt;/asp:EntityDataSource&gt;
                 &lt;/InsertItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:BoundField HeaderText="WOGeneratedDate" DataField="WOGeneratedDate" DataFormatString="yyyy-MM-dd" /&gt;
              .......
          &lt;/Fields&gt;
      &lt;/asp:DetailsView&gt;
  
     &lt;asp:ObjectDataSource ID="ODSAddWorkOrder" runat="server"
          DataObjectTypeName="MiantenanceManager.DAL.WorkOrder" TypeName="MiantenanceManager.BLL.WorkOrderBL"
          InsertMethod="addWorkOrder"&gt;
      &lt;/asp:ObjectDataSource&gt;
  </code></pre>
  
  <p>And add the following code to DetailView ItemInserting method.</p>
  
  <pre><code>    protected void DVWorkOrder_ItemInserting(object sender, DetailsViewInsertEventArgs e)
              {
                  if (_CBIsWOOfComplain.Checked)
                  {
                      using (complainBL)
                      {                    
                          e.Values["Complain"] = complainBL.getComplianByID(_DDLComplains.SelectedValue);
                      }
                  }
              }
  </code></pre>
  
  <p>Note: complainBL implements the IDispose.
  In WorkOrederBL call it calls the WorkOrderRepository class which invokes the following method which throws the exception mention in the subject.</p>
  
  <p>context.WorkOrder.AddObject(workOrder);
                 context.SaveChanges();</p>
  
  <p>How can i overcome this problem. Is my approch is wrong?? </p>