2013-09-05 26 views
1

我正在通过基于.NET 4.0中的WCF DataServices的OData提要从我的数据库中暴露一个实体。到目前为止,所有事情都已完全公开,但我现在正在限制实体可能进行的操作。防止修改实体上的特定属性

我有一个Order对象具有这些属性(其中包括):

ID  
Name  
Amount  
CustomerID 

我希望能够将所有值暴露给服务的消费者,让他们对其进行更新。但是,我不希望他们能够更新实体的CustomerID属性。

我该如何做到这一点?我查看了QueryInterceptors,但是我还没有找到阻止更新调用或修改请求的正确方法。

回答

1

您可以用ChangeInterceptor

[ChangeInterceptor("Orders")] 
public void OnChangeOrders(Order order, UpdateOperations operations) 
{ 
    if (operations == UpdateOperations.Change) 
    { 
     //Get the record as it exists before the change is made 
     var oldValue = CurrentDataSource.ChangeTracker.Entries<Order>().First(); 

     //You can compare the various properties here to determine what, if anything, 
     //has changed, or just write over the property if you want 

     order.CustomerID = oldValue.Entity.CustomerID; 

    } 
} 
做到这一点