2011-03-04 74 views
1

我有一個WCF數據服務正在運行,它正在公開EDM。我需要在客戶端有幾個屬性,數據庫不需要知道。設置完所有後,我需要測試SaveContext方法,並在服務器上獲取此錯誤「處理請求流錯誤。爲'DataModels.Customer'類型指定的屬性名'CanDelete'無效。WCF數據服務客戶端上的部分類屬性導致SaveContext異常

有沒有辦法告訴WCF數據服務在客戶端忽略此屬性?或者我應該轉向RIA Serivces?我讀過將屬性設置爲internal會執行此操作,但我需要該屬性進行綁定,並且在不同的項目中有客戶端UI代碼(將我的SL應用程序從我的數據服務中解除耦合)。

在客戶端上

我:

public partial class Customer 
{ 
    private bool canDelete; 

     /// <summary> 
     /// Gets or sets a value indicating whether this instance can be deleted. 
     /// </summary> 
     /// <value> 
     ///  <c>true</c> if this instance can delete; otherwise, <c>false</c>. 
private bool canDelete; 

/// <summary> 
/// Gets or sets a value indicating whether this instance can be deleted. 
/// </summary> 
/// <value> 
///  <c>true</c> if this instance can delete; otherwise, <c>false</c>. 
/// </value> 
public bool CanDelete 
{ 
    get 
    { 
     return this.canDelete; 
    } 

    set 
    { 
     if (this.canDelete != value) 
     { 
      this.canDelete = value; 
      this.OnPropertyChanged("CanDelete"); 
     } 
    } 
} 
} 

回答

1

我有完全相同的問題,並從 extending partial designer classes

適應下面的一些代碼,它只是涉及鉤住WritingEntity事件的分部類的語境裏。

我添加了自己的屬性(IgnorePropertyAttribute),所以我可以將它附加到其他屬性。

當然會很好,如果屬性wasnt插入擺在首位,但這個工作對我來說

public sealed class IgnorePropertyAttribute : Attribute 
{ 
} 

...

partial void OnContextCreated() 
    { 
     this.WritingEntity += MyDataContext_WritingEntity; 
    } 

    private void MyDataContext_WritingEntity(object sender, System.Data.Services.Client.ReadingWritingEntityEventArgs e) 
    { 
     // 
     foreach (XElement node in e.Data.Elements()) 
     { 
      if (node != null && node.Name.LocalName == "content") 
      { 
       foreach (XElement el in node.Elements()) 
       { 
        if (el.Name.LocalName == "properties") 
        { 
         foreach (XElement prop in el.Elements()) 
         { 
          if(e.Entity.GetType().GetProperty(prop.Name.LocalName).GetCustomAttributes(typeof(IgnorePropertyAttribute), true).Length > 0) 
          { 
           prop.Remove(); 
          } 
         } 
        } 
       } 
      } 
     } 
    }