1

我有以下分层的代码:

  1. 数据 - DbContext 数据库访问。
  2. 实体 - 具有 INotifyPropertyChanged 的​​ POCO 实体
  3. WCF 服务 - 使用数据和实体
  4. 测试库

这是来自测试方法的代码。

        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;}
}

我的实体库

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

我在这里想念什么?谢谢你的帮助!

4

1 回答 1

3

我通过将 [DataContract(IsReference = true)] 添加到公共部分类 SalesOrder: EntityBase 和公共部分类 SalesOrderDetail: EntityBase 来修复它。不知道它是如何产生影响的。但这解决了我的问题。

于 2013-04-25T17:20:18.373 回答