我正在使用带有SQLCE 4.0的芒果构建Windows Phone应用程序(我认为)。
当我尝试使用context.SubmitChanges()
命令提交新的OrderItem时出现此错误。
无法将重复值插入唯一索引。 [表 name = Order,Constraint name = PK_Order]
以下是一些代码:
[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");
}
}
}
我看不出我做错了什么。
这是L2SQL代码:
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;
}
我希望有人可以帮助我,我已经花了很多时间解决这个问题!
编辑:
我试过这段代码:
context.OrderItems.InsertOnSubmit(new OrderItem { Order = (new Order { OrderId = 1234567 }), ProductId = "sdfsdf3dsf", Quantity = Quantity });
context.SubmitChanges();
我得到了同样的错误,但当我将OrderId更改为它的工作原理时。问题是,如果我在该订单上提交一个orderitem,它将是orderId,所以我猜这个关联有问题吗?