"A duplicate value cannot be inserted into a unique index. [ Table name = Order,Constraint name = PK_Order ]"

2.7k views Asked by At

I am building a Windows Phone Application using Mango with SQLCE 4.0 (I think).

I get this error when I am trying to submit a new OrderItem, with context.SubmitChanges() command.

A duplicate value cannot be inserted into a unique index. [Table name = Order,Constraint name = PK_Order]

Here is some code:

[Table]
public partial class Order : BCSDataContextBase //Base only icludes NotifyProperty....
{
    private int _id;
    private int _orderId;
    private EntitySet<OrderItem> _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<OrderItem> 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<OrderItem>(
            new Action<OrderItem>(this.attach_Item),
            new Action<OrderItem>(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<Order> _order;

    public OrderItem() 
    {
        this._order = default(EntityRef<Order>);
    }

    [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");
        }
    }
}    

I cant see what I am doing wrong.

Here's the L2SQL code:

public bool SaveOrderItemByOrder(OrderItem newItem)
    {
        bool successfullySaved = false;
        using (var context = DataObjectFactory.CreateContext())            
        {
            var item = db.OrderItems.Where(i => 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;
    }

I Hope someone can help me out, i have already put to many hours on this problem!

EDIT:

I tried this code:

context.OrderItems.InsertOnSubmit(new OrderItem { Order = (new Order { OrderId = 1234567 }), ProductId = "sdfsdf3dsf", Quantity = Quantity });
        context.SubmitChanges();

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 ?

0

There are 0 answers