How to access a backing field in a base class?
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?
2 Posted by Devon Lazarus on 27 May, 2010 03:15 PM
_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 aSubclassMap<Article>
. Something like this:This could be different depending on whether you're implementing Table-per-class-hierarchy or Table-per-subclass.
hth,
-devon