Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHIbernate readonly property - fluent mapping

I have the next entity:

public class Topic : EntityBase
{
    private readonly ICollection<Vote> _votes;

    public virtual string Title { get; protected set; }
    public virtual ICollection<Vote> Votes
    {
        get { return _votes; }
    }

    public virtual int VotedUpCount
    {
        get
        {
            return _votes.Count(v => v.VotedTo == VoteType.VoteUp);
        }
    }
}

I need to map my VotedUpCount (because I have an error from NH). BUT I don't need this property as column in my database.

I tried this one and got an error Invalid column name 'VotedUpCount'.

public class TopicMap : ClassMap<Topic>
{
    public TopicMap()
    {
        Id(x => x.Id);
        Map(x => x.Title);
        Map(x => x.VotedUpCount).Access.ReadOnly();
        HasMany<Vote>(x => x.Votes)
            .Cascade.All();
    }
}

Is there any way to map readonly properties using fluent interface to make this entity working?

like image 889
Akim Khalilov Avatar asked Feb 21 '23 17:02

Akim Khalilov


1 Answers

Why do you want to map it? It's just a .NET property executing some code (that could also be a method) and has nothing to do with the database, just don't map it at all.

If you need a real DB property (where the value comes from a DB column) mapped as readonly:

Map(x => x.PropertyName).Column("ColumnName").ReadOnly();
// Alternative (does exactly the same, but is more like the NHibernate mapping:
Map(x => x.PropertyName).Column("ColumnName").Not.Insert().Not.Update();
like image 108
cremor Avatar answered Mar 03 '23 16:03

cremor