Reputation: 3515
I have Page entity with properties Keyword and Keywords.
Keyword
property store one string like string Keyword = "my awesome page, page";
Keyword will store this information inside database.
Keywords on the other hand retrieve this keyword content and separating with commas.
so I have inside my model
public Page : Entity<int>
{
public virtual string Keyword {get; set;}
public virtual IList<string> Keywords
{
get { return Keyword.Split(','); }
set { Keyword = string.Join(",", value); }
}
public Page() { Keywords = new List<string>(); }
}
so I tried to map this entity with conformist mapping by code
public class PageMap : ClassMapping<Page>
{
public PageMap()
{
Property(x => x.Keyword);
Property(x => x.Keywords, m =>
{
m.Access(Accessor.Field);
});
}
}
but I'm getting expcetion like
NHibernate.MappingException : Could not compile the mapping document: mapping_by_code ----> NHibernate.MappingException : Problem trying to set property type by reflection NHibernate.PropertyNotFoundException : Could not find property nor field 'Keywords' in class 'Model.Page'
Even I understand message that property cannot be found I do not understand why? and how to overcome this.
thanks
Upvotes: 2
Views: 2320
Reputation: 123901
There are two issues. First, the Keywords property is "virtual", meaning that it is not persisted, it is calculated/built in runtime. To make it working you do not need to map and therefore to store it. Just remove the Keywords mapping...
The Second thing is the mapping. In case, that Keywords
won't be virtual (runtime resolved based ont the Keyword
property value), we have to map it as collection. Any collection (e.g. IList<string>
) represents one-to-many relation, (in fluent called HasMany):
<bag name="Keywords" table="Keywords">
<key column="PageId" />
<element column="Keyword" />
</bag>
This will store all keywords in its own table (e.g. Keywords), referenced by PageId
column and persisted in a column (e.g. nvrachar
) called Keyword
. The Fluent syntax would be like:
HasMany(x => x.Keywords)
.Table("Keywords")
.KeyColumn("PageId")
.Element("Keyword");
But, as already said: if the Keywords
is virutal, do not map it.
Upvotes: 1