我正在使用带有 SQLCE 4.0 的 Mango(我认为)构建 Windows Phone 应用程序。
当我尝试使用context.SubmitChanges()
命令提交新的 OrderItem 时出现此错误。
不能将重复值插入唯一索引。[表名 = 订单,约束名 = 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 更改为它可以工作的任何内容时。问题是,如果我在该订单上提交一个订单项,它将是那个 orderId,所以我猜关联中存在一些问题?