Fluent NHibernate and computed properties

后端 未结 3 1249
余生分开走
余生分开走 2021-01-13 18:48

I\'m using Fluent NHibernate, and auto-mapping the classes.

I have a computed property in a class along the lines of

public virtual DateTime? LastAct         


        
3条回答
  •  余生分开走
    2021-01-13 19:23

    You could associate a formula with the property and use that instead of the c# code. e.g.

    Property:

    private int _postCount = 0;
    public virtual int PostCount
    {
        get
        {
            return _postCount;
        }
    }
    

    Formula:

    (SELECT count(p.ID) FROM BlogPost p WHERE p.BlogID = ID and p.IsDeleted = 0)
    

    And then you can use PostCount in your expression as usual. Remember to set the access modifier on the property in your FluentMapping. Not sure what Fluent supports but the options I have found for normal mapping are:

    * property
    * field
    * field.camelcase
    * field.camelcase-underscore
    * field.pascalcase-m-underscore
    * field.lowercase-underscore
    * nosetter.camelcase
    * nosetter.camelcase-underscore
    * nosetter.pascalcase-m-underscore
    * nosetter.lowercase-underscore
    

    Probably best check out NHibernate documentation for the official list Access Strategies so you combine the access strategy with the naming strategy e.g. "field.lowercase-underscore"

提交回复
热议问题