简体   繁体   English

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

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

I have an abstract class that's autogenerated by a code template. 我有一个由代码模板自动生成的抽象类。 We then have several classes that derive from this class. 然后,我们从该类派生了几个类。 There is a particular property on that class that for one of the derived implementations I would like to override the getter and setter. 该类上有一个特殊的属性,对于一个派生的实现,我想覆盖getter和setter。 Unfortunately I can find no way of overriding the property as it is not declared virtual. 不幸的是,由于没有将其声明为虚拟属性,因此无法覆盖该属性。

So as another approach, I decided to make the property protected, and then in the partial class ( .shared.cs ) create a public virtual property that effectively wraps the protected one. 因此,作为另一种方法,我决定将属性设置为受保护,然后在局部类( .shared.cs )中创建一个有效包装受保护的属性的公共虚拟属性。 Then I can override this in the one specific implementation. 然后,我可以在一个特定的实现中覆盖它。

So on the server side this looks fine, but once I build it, it turns out that the partial shared file that ria generates for me on the client seems to have no visibility of the protected property. 因此,在服务器端看起来不错,但是一旦构建它,事实证明ria在客户端上为我生成的部分共享文件似乎看不到受保护的属性。

ClassA.cs : 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;
            }
        }
    }
}

This effectively gives the error '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?) 这有效地导致错误'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?)

When double clicking the error it takes me to the client version of the file, where for some reason it can not see that protected property. 当双击错误时,将我带到该文件的客户端版本,由于某种原因,它无法看到该受保护的属性。

Any idea why? 知道为什么吗? or alternatively is there a way (using Database first) to mark a field so it generates as virtual? 还是可以选择一种方法(首先使用数据库)来标记一个字段,使其生成为虚拟字段?

WCF RIA does not create a member in the Web.g.cs unless the member would have been serialized. 除非该成员已被序列化,否则WCF RIA不会在Web.g.cs创建成员。 As ApplicationNumber is a protected property, WCF RIA ignores it. 由于ApplicationNumber是受保护的属性,因此WCF RIA会将其忽略。 This explains why it compiles in the web project, but not in Silverlight. 这就解释了为什么要在Web项目中而不是在Silverlight中进行编译。

Have you tried not sharing the other partial but adding the property instead? 您是否尝试过共享其他部分,而是添加属性?

Change it to ClassA.cs or ClassA.partial.cs and the contents to: 将其更改为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