Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 475917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:26:12+00:00 2026-05-13T00:26:12+00:00

I have entities Post and Tag , where there is a many-to-many relationship between

  • 0

I have entities Post and Tag, where there is a many-to-many relationship between the two (e.g. each post can have one or more tags, and each tag can be associated with any number of posts).

What I would like to achieve is to have the Tag entity to have a field providing the number of posts which are associated with that tag. However, I don’t know how to go about getting this without getting the entire collection of Posts (which I’d like to avoid).

I am using Fluent NHibernate and my entities and mappings look like this currently:

Entities/Post.cs

public class Post : PersistentBase
{
    public virtual string Title { get; set; }
    /* snip */

    private IList<Tag> tags = new List<Tag>();
    public virtual IEnumerable<Tag> Tags {
        get { return tags; }
    }

    public virtual void AddTag(Tag tag) {
        this.tags.Add(tag);
    }
}

Mappings/PostMap.cs

public class PostMap : ClassMap<Post>
{
    public PostMap()
    {
        Id(x => x.Id).GeneratedBy.HiLo("99");
        Map(x => x.Title);
        /* snip */
        HasManyToMany(x => x.Tags);
    }
}

Entities/Tag.cs

public class Tag : PersistentBase
{
    public virtual string Name { get; set; }

    public static Tag Create(string name) {
        return new Tag { Name = name };
    }
}

Mappings/TagMap.cs

public class TagMap : ClassMap<Tag>
{
    public TagMap ()
    {
        Id(x => x.Id).GeneratedBy.HiLo("99");
        Map(x => x.Name).Unique();
    }
}

Ideally what I’d like to achieve is to be able to add into Entities/Tag.cs, something like the following:

public virtual int PostCount { get; set; }

And then have that pre-filled with the number of posts using that tag.

How might I go about doing this? (is it indeed possible?)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:26:12+00:00Added an answer on May 13, 2026 at 12:26 am

    What you need is to define a formula in your Tag mapping.

    Here is how the Tag entity mapping should look like in XML (I am sure you can translate that to the Fluent NHibernate way):

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>
        <class name="Tag" table="Tag">
            <id name="Id" type="Int32" column="TagID">
                <generator class="hilo" />
            </id>
    
            ...
    
            <property name="Name" column="Name" type="String" not-null="true" />
            <property name="PostCount" formula="(select count(*) from PostTag pt where pt.TagID = TagID)" type="Int32" insert="false" update="false" />
    
            ...
        </class>    
    </hibernate-mapping>
    

    This is assuming that your association table between Post and Tag in your Database is called PostTag. Please change the name accordingly to suit your needs.

    Formulas work on the database level so are written in SQL and not HQL therefore the column names are used instead of the property names as you will notice in the XML above.

    Then as you described you will need in the Tag.cs to add aproperty like this:

    public virtual int PostCount { get; protected set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities: Location Post It's a 1.. * between Location and Post
Imagine you have two entities, Player and Team , where players can be on
Let's assume I have two entities - project team and employee. Each employee could
Having two classes like Blog and Post, in Entity Framework (and LINQ-to-Entities), how can
I am storing tags to posts as a many-to-many relationship, like in this post
How can i create a relationship between entities with Symfony 2 and Doctrine? I'm
If I have entities set up for Parent - Child - Grandchild and one
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I have two entities A and B. public class A{ @Id @GeneratedValue private Integer
I have two entities Visita and Cliente that implements respectively two interfaces IVisita and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.