客户端上的 WCF 数据服务部分类属性导致 SaveContext 异常

发布于 2024-10-20 18:50:17 字数 1166 浏览 6 评论 0原文

我正在运行一个公开 EDM 的 WCF 数据服务。我在客户端需要一些属性,而数据库不需要知道这些属性。设置完所有这些后,我开始测试 SaveContext 方法并在服务器上收到此错误“处理请求流时出错。为类型“DataModels.Customer”指定的属性名称“CanDelete”无效。”

有没有办法告诉客户端的 WCF 数据服务忽略此属性?或者我应该转向 RIA 服务?我读过,将属性设置为内部即可执行此操作,但我需要该属性进行绑定,并且我在不同的项目中拥有客户端 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");
        }
    }
}
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-10-27 18:50:17

我遇到了完全相同的问题并改编了下面的一些代码
扩展部分设计器类

只需将 WriteEntity 事件挂接到 Context 的部分类中。

我添加了自己的属性 (IgnorePropertyAttribute),以便可以将其附加到其他属性。

如果首先没有插入该属性当然会很好,但这对我有用

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

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();
                            }
                        }
                    }
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文