How to access a backing field in a base class?

Original Post atp0303's Avatar

atp0303

25 May, 2010 04:02 PM via web

I would like to access a backing field which is defined in a base class:

 public class Article : Content
 {
       public string Body {get; set;} 
 }

 public class Content
 { 
       private IList _photos; 
       public Content() 
       {
            _photos = new List<Photo>();
       }   

       public virtual IEnumerable Photos 
       {
          get {  return _photos;  }
       }  

       public virtual void AddPhoto() {...} 
}

My mapping class is something like:

  public ArticleMap()
  {  
      Id(x => x.Id);
      Map(x => x.Body);                        
      HasMany(x => x.Photos)
        .Access.CamelCaseField(Prefix.Underscore)
        .Cascade.All();
  }

I constantly get the error that the _photos field cannot be found. Even marking the _photos field as public does not work.

How can i tell the configuration to "look" in the base class to find _photos?

  1. 2 Posted by Devon Lazarus on 27 May, 2010 03:15 PM

    Devon Lazarus's Avatar

    _photos is marked as a private variable in the base class so it is not accessible from the subclass. Try accessing base._photos directly in your Article class and you'll see what I mean.

    You need to map the base class, then turn ArticleMap into a SubclassMap<Article>. Something like this:

    public ContentMap : ClassMap<Content> {
        HasMany(x => x.Photos)
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.All();
    }
    
    public ArticleMap : SubclassMap<Article> {
        Id(x => x.Id);
        Map(x => x.Body);
    }
    

    This could be different depending on whether you're implementing Table-per-class-hierarchy or Table-per-subclass.

    hth,

    -devon

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What comes next? 'Monday Tuesday Wednesday ?????'

    Recent Discussions

    05 Jul, 2010 10:29 PM
    05 Jul, 2010 12:45 PM
    05 Jul, 2010 12:42 PM
    05 Jul, 2010 12:17 PM
    05 Jul, 2010 12:12 PM

     

    03 Jul, 2010 12:26 AM
    02 Jul, 2010 02:17 PM
    02 Jul, 2010 08:18 AM
    02 Jul, 2010 12:20 AM
    01 Jul, 2010 10:14 PM