【问题标题】:Entity Framework & RIA Services - cant access protected property in shared class on client实体框架和 RIA 服务 - 无法访问客户端共享类中的受保护属性
【发布时间】:2013-08-28 16:00:08
【问题描述】:

我有一个由代码模板自动生成的抽象类。 然后我们有几个派生自这个类的类。 该类有一个特定的属性,对于其中一个派生实现,我想覆盖 getter 和 setter。 不幸的是,我找不到覆盖该属性的方法,因为它没有被声明为虚拟的。

因此,作为另一种方法,我决定使属性受保护,然后在部分类 (.shared.cs) 中创建一个公共虚拟属性,该属性有效地包装了受保护的属性。然后我可以在一个特定的实现中覆盖它。

所以在服务器端这看起来不错,但是一旦我构建它,事实证明 ria 在客户端为我生成的部分共享文件似乎看不到受保护的属性。

ClassA.cs:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behaviour in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ABC.Web.Models.DomainModel
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    [RoundtripOriginal]
    public abstract partial class ClassA
    {
        public int Id { get; set; }
        public string Title { get; set; }
        protected string ApplicationNumber { get; set; }
    }
}

ClassA.shared.cs

namespace ABC.Web.Models.DomainModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;

    public abstract partial class ClassA
    {
        [IgnoreDataMember]
        public virtual string ApplicationNumberAccessor
        {
            get
            {
                return this.ApplicationNumber;
            } 
            set
            {
                this.ApplicationNumber = value;
            }
        }
    }
}

这实际上给出了错误'ABC.Web.Models.DomainModel.ClassA' does not contain a definition for 'ApplicationNumber' and no extension method 'ApplicationNumber' accepting a first argument of type 'ABC.Web.Models.DomainModel.ClassA' could be found (are you missing a using directive or an assembly reference?)

双击错误时,它会将我带到文件的客户端版本,由于某种原因它看不到该受保护的属性。

知道为什么吗?或者有没有办法(首先使用数据库)标记一个字段,使其生成为虚拟?

【问题讨论】:

    标签: c# entity-framework silverlight wcf-ria-services


    【解决方案1】:

    WCF RIA 不会在 Web.g.cs 中创建成员,除非该成员已被序列化。由于ApplicationNumber 是受保护的属性,WCF RIA 会忽略它。这解释了为什么它会在 Web 项目中编译,但不能在 Silverlight 中编译。

    您是否尝试过不共享另一个部分,而是添加属性?

    将其更改为ClassA.cs 或ClassA.partial.cs 并将内容更改为:

    namespace ABC.Web.Models.DomainModel
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Runtime.Serialization;
    
        public abstract partial class ClassA
        {
            // You _do_ want this serialized to the client and back
            // so remove the [IgnoreDataMember] atribute
            public virtual string ApplicationNumberAccessor
            {
                get
                {
                    return this.ApplicationNumber;
                } 
                set
                {
                    this.ApplicationNumber = value;
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢埃德!有 IgnoreDataMember 因为它应该从 ApplicationNumber 获取值,所以没有必要发送两次值。但是这样它仍然只发送一次。 ApplicationNumber 不需要存在于客户端,因为当 RIA 将访问器值发送回服务器时,它将在服务器类上设置访问器值,这将更新 ApplicationNumber 并因此得到正确持久化。现在看起来很明显,你帮我看清楚了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多