1
我有以下的代碼,該代碼如下分層:導航屬性爲空,而調用WCF方法
- 數據 - 的DbContext數據庫訪問。
- 實體 - 與INotifyPropertyChanged的
- WCF服務POCO實體 - 使用數據和實體
- 測試庫
下面是測試方法的代碼。
SalesOrder order = new SalesOrder();
order.CustomerID = 1;
order.OrderDate = DateTime.Now;
order.SalesTax = 5.60M;
order.ShipCharge = 5.00M;
order.DiscountPercent = 20;
order.ModifiedByUser = Environment.UserName;
order.CreatedByUser = Environment.UserName;
SalesOrderDetail detail = new SalesOrderDetail();
detail.SalesOrder = order;
detail.OrderID = order.OrderID;
detail.OrderQuantity = 15;
detail.UnitPrice = 20;
detail.Description = "Test";
detail.ModifiedByUser = Environment.UserName;
detail.CreatedByUser = Environment.UserName;
order.SalesOrderDetails.Add(detail);
order = _service.SaveOrder(order);
order.SalesOrderDetails此時有一個項目。但是,當它到達WCF方法,order.SalesOrderDetails具有0
這裏計數是我SalesOrder類的SalesOrderDetail類
public partial class SalesOrder: EntityBase
{
public SalesOrder()
{
this.SalesOrderDetails = new BindingList<SalesOrderDetail>();
((BindingList<SalesOrderDetail>)SalesOrderDetails).ListChanged += SalesOrder_ListChanged;
}
.... other properties
[DataMember]
public ICollection SalesOrderDetail{get; set;}
}
public partial class SalesOrderDetail : EntityBase
{
.... other properties
[DataMember]
public SalesOrder SalesOrder{get; set;}
}
我EntityBase
[DataContract(IsReference = true)]
public class EntityBase:IAuditable, INotifyPropertyChanged
{
.... IAuditable, INotify members
}
我的WCF方法
public SalesOrder SaveOrder(SalesOrder order) // At this point order.SalesOrderDetails count is 0.
{
if (order.OrderID == 0)
AddSalesOrder(order);
else
UpdateSalesOrder(order);
return order;
}
我在這裏錯過了什麼?謝謝你的幫助!
你的錯誤是什麼? – 2013-04-24 13:21:40
沒有錯誤。但order.SalesOrderDetails計數是0(非空),當它是公共SalesOrder SaveOrder(SalesOrder訂單)。因此沒有記錄插入SalesOrderDetails。但訂單正在創建。 – isakavis 2013-04-24 14:03:54
我通過向公共部分類SalesOrder:EntityBase和公共部分類SalesOrderDetail:EntityBase添加[DataContract(IsReference = true)]來修復它。不知道它是如何有所作爲。感謝您的幫助 – isakavis 2013-04-25 14:23:07