开发者

WCF Data Service Partial class property on the client causes SaveContext exceptions

I have a WCF Data Service running that is exposing an EDM. There are several properties I needed on the client side, that the database doesn't need to know about. After setting all that up I got to testing the SaveContext method and get this error on the server "Error 开发者_运维知识库processing request stream. The property name 'CanDelete' specified for type 'DataModels.Customer' is not valid."

Is there a way to tell WCF Data Services on the client side to ignore this property? Or should I move to RIA Serivces? I've read that setting the property to internal will do this, but I need the property for binding and I have the client UI code in a different project (de-coupling my SL applications from my data service).

on the client I have:

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


I had the exact same problem and adapted some code below from extending partial designer classes

It simply involves hooking the WritingEntity Event inside a partial class of the Context.

I added my own attribute (IgnorePropertyAttribute) so I could attach it to other properties.

Would of course be nice if the attribute wasnt inserted in the first place but this worked for me

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();
                            }
                        }
                    }
                }
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜