防止修改实体上的特定属性

时间:2013-09-05 12:43:21

标签: wcf-data-services odata

我通过基于.Net 4.0中的WCF DataServices的OData源从我的数据库中公开实体。到目前为止,一切都已完全开放,但我现在正在限制实体可能的操作。

我有一个Order对象具有这些属性(以及其他):

ID    
Name    
Amount    
CustomerID

我希望能够将所有值公开给服务的使用者,并允许他们更新它们。但是,我不希望他们能够更新实体的CustomerID属性。

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

1 个答案:

答案 0 :(得分: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;

    }
}
相关问题