繁体   English   中英

实体框架和RIA服务-无法在客户端的共享类中访问受保护的属性

[英]Entity Framework & RIA Services - cant access protected property in shared class on client

我有一个由代码模板自动生成的抽象类。 然后,我们从该类派生了几个类。 该类上有一个特殊的属性,对于一个派生的实现,我想覆盖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?)

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

知道为什么吗? 还是可以选择一种方法(首先使用数据库)来标记一个字段,使其生成为虚拟字段?

除非该成员已被序列化,否则WCF RIA不会在Web.g.cs创建成员。 由于ApplicationNumber是受保护的属性,因此WCF RIA会将其忽略。 这就解释了为什么要在Web项目中而不是在Silverlight中进行编译。

您是否尝试过共享其他部分,而是添加属性?

将其更改为ClassA.csClassA.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;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM