【问题标题】:Preventing modification of specific properties on entity防止修改实体上的特定属性
【发布时间】:2017-03-15 01:11:58
【问题描述】:

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

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

ID    
Name    
Amount    
CustomerID

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

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

【问题讨论】:

    标签: wcf-data-services odata


    【解决方案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;
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-15
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多