title: "A duplicate value cannot be inserted into a unique index. [ Table name = Order,Constraint name = PK_Order ]"
body: <p>I am building a Windows Phone Application using Mango with SQLCE 4.0 (I think).</p>
  
  <p>I get this error when I am trying to submit a new OrderItem, with <code>context.SubmitChanges()</code> command.</p>
  
  <blockquote>
    <p>A duplicate value cannot be inserted into a unique index. [Table
    name = Order,Constraint name = PK_Order]</p>
  </blockquote>
  
  <p>Here is some code:</p>
  
  <pre><code>[Table]
  public partial class Order : BCSDataContextBase //Base only icludes NotifyProperty....
  {
      private int _id;
      private int _orderId;
      private EntitySet&lt;OrderItem&gt; _items;
      private DateTime _dateCreated = DateTime.Now;
      private DateTime _dateModified = DateTime.Now;
  
      [Column(IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
      public int Id
      {
          get { return _id; }
          set {
              if (_id != value) {
                  NotifyPropertyChanging("Id");
                  _id = value;
                  NotifyPropertyChanged("Id");
              }
          }
      }
  
      [Column(IsPrimaryKey = true, DbType = "INT NOT NULL", CanBeNull = false)]
      public int OrderId
      {
          get { return _orderId; }
          set 
          {
              if (_orderId != value) {
                  NotifyPropertyChanging("OrderId");
                  _orderId = value;
                  NotifyPropertyChanged("OrderId");
              }
          }
      }
  
      [Association(Storage = "_items", ThisKey="OrderId", OtherKey = "OrderId")]
      public EntitySet&lt;OrderItem&gt; Items
      {
          get { return this._items; }
          set { this._items.Assign(value); }
      }                
  
      // Version column aids update performance.
      [Column(IsVersion = true)]
      private Binary _version;
  
      public Order()
      {
          this._items = new EntitySet&lt;OrderItem&gt;(
              new Action&lt;OrderItem&gt;(this.attach_Item),
              new Action&lt;OrderItem&gt;(this.detach_Item)
              );
      }
  
      private void attach_Item(OrderItem entity)
      {
          NotifyPropertyChanging("OrderItem");
          entity.Order = this;
      }
  
      private void detach_Item(OrderItem entity)
      {
          NotifyPropertyChanging("OrderItem");
          entity.Order = null;
      }
  }
  
  [Table]
  public partial class OrderItem : BCSDataContextBase
  {
      private int _id;
      private int _orderId;
      private string _productId;
      private int _quantity;        
      private EntityRef&lt;Order&gt; _order;
  
      public OrderItem() 
      {
          this._order = default(EntityRef&lt;Order&gt;);
      }
  
      [Column(IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
      public int Id
      {
          get { return _id; }
          set
          {
              if (_id != value) {
                  NotifyPropertyChanging("Id");
                  _id = value;
                  NotifyPropertyChanged("Id");
              }
          }
      }
  
      [Column(Storage = "_orderId", DbType = "Int NOT NULL", AutoSync = AutoSync.OnInsert)]        
      public int OrderId
      {
          get { return this._orderId; }
          set
          {
              if (this._orderId != value)
              {
                  NotifyPropertyChanging("OrderId");
                  this._orderId = value;
                  NotifyPropertyChanged("OrderId");
              }
          }
      }
  
      [Column(Storage = "_productId", IsPrimaryKey = true)]
      public string ProductId
      {
          get { return _productId; }
          set
          {
              if (_productId != value)
              {
                  NotifyPropertyChanging("ProductId");
                  _productId = value;
                  NotifyPropertyChanged("ProductId");
              }
          }
      }
  
      [Column(Storage = "_quantity", DbType = "Int NOT NULL")]
      public int Quantity
      {
          get { return this._quantity; }
          set
          {
              if (this._quantity != value)
              {
                  NotifyPropertyChanging("Quantity");
                  this._quantity = value;
                  NotifyPropertyChanged("Quantity");
              }
          }
      }                       
  
      // Entity reference, to identity the Order "storage" table
      [Association(Name = "Order_Order_Item", Storage = "_order", ThisKey = "OrderId", OtherKey = "OrderId", IsForeignKey = true)]
      public Order Order
      {
          get { return this._order.Entity; }
          set
          {
              NotifyPropertyChanging("Order");                
              this._order.Entity = value;
  
              if (value != null)
              {
                  value.Items.Add(this);
                  this._orderId = value.OrderId;
              }
              else { this._orderId = default(int); }
  
              NotifyPropertyChanging("Order");
          }
      }
  }    
  </code></pre>
  
  <p>I cant see what I am doing wrong.</p>
  
  <p>Here's the L2SQL code:</p>
  
  <pre><code>public bool SaveOrderItemByOrder(OrderItem newItem)
      {
          bool successfullySaved = false;
          using (var context = DataObjectFactory.CreateContext())            
          {
              var item = db.OrderItems.Where(i =&gt; i.ProductId == newItem.ProductId).FirstOrDefault();
  
              try
              {
                  if (item != null)
                  {
                      item.Quantity = newItem.Quantity;                        
                      db.SubmitChanges();
                  }
                  else
                  {                        
                      db.OrderItems.InsertOnSubmit(newItem);
                      db.SubmitChanges();                    
                  }
  
                  successfullySaved = true;
              }
              catch (Exception ex)
              {
                  throw ex;
                  successfullySaved = false;
              }
  
          }
  
          return successfullySaved;
      }
  </code></pre>
  
  <p>I Hope someone can help me out, i have already put to many hours on this problem!</p>
  
  <p>EDIT:</p>
  
  <p>I tried this code:</p>
  
  <pre><code>context.OrderItems.InsertOnSubmit(new OrderItem { Order = (new Order { OrderId = 1234567 }), ProductId = "sdfsdf3dsf", Quantity = Quantity });
          context.SubmitChanges();
  </code></pre>
  
  <p>And i got same error, but when I changed the OrderId to whatever it works. The problem is that if I submitting an orderitem on that order, it will be that orderId, so I guess there are some problem in the association ?</p>