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 4591498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:24:14+00:00 2026-05-21T22:24:14+00:00

i have 3 tables in my database: Projects (id, name) Tags (id, name) ProjectsTagss

  • 0

i have 3 tables in my database:

  1. Projects (id, name)
  2. Tags (id, name)
  3. ProjectsTagss (id, projectId, tagid)

As you can see the ProjectsTags table is a bridge table

here is my fluent nhibernate mapping

ProjectMap.cs:

 Map(x => x.Name).Not.Nullable();
 HasMany(x => x.ProjectsTags).AsBag().Inverse()
    .Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(80);

ProjectsTagsMap.cs:

 References(x => x.Project).Not.Nullable();
 References(x => x.Tag).Not.Nullable();

TagMap.cs:

  Map(x => x.Name).Not.Nullable();

As you can see, i historically didn’t have the Tag table linked to anything else. I now need to generate a report to show Tag and how often that tag is used so i need to join from Tag to ProjectsTag. i tried adding this line into the tagsmap:

 HasMany(x => x.ProjectsTags).AsBag().Inverse()
    .Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(80);

but when i go to update the name on a tag object and commit, i get this error:

A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

can anyone see anything wrong with what i added that would be causing this nhibernate exception when i simply update the Tag table. Again my goal is to be able to do something like:

 Tag.ProjectTags.Count();

Here is some additional code as requested:

my Tag Class:

 public class Tag
{
    public virtual IList<ProjectTag> ProjectTags { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 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-21T22:24:15+00:00Added an answer on May 21, 2026 at 10:24 pm

    While a collection is not modified, NH can still think that it is. Something like this could be caused by a ghost update. From NHibernate 3.0 Cookbook, Jason Dentler (page 184): “As part of automatic dirty checking, NHibernate compares the original state of an entity to
    its current state. An otherwise unchanged entity may be updated unnecessarily because a
    type conversion caused this comparison to fail”.

    Ghost update of collection can be caused by code that looks like this:

    public class Tag
    {
        private IList<ProjectTag> projectsTags;
    
        public virtual IEnumerable<ProjectTag> ProjectsTags
        {
            get
            {
                return new ReadOnlyCollection<ProjectTag>(projectsTags);
            }
    
            set
            {
                projectsTags = (IList<ProjectTag>)value;
            }
        }
    }
    

    ProjectsTags property returns the collection in readonly wrapper, so client code cannot add or remove elements to/from the collection.

    The error will appear even when name of a tag is not changed:

    private void GhostTagUpdate(int id)
    {
        using (var session = OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                var tag = session.Get<Tag>(id);
    
                transaction.Commit();
            }
        }
    }
    

    ProjectsTags collection should be mapped with CamelCaseField access strategy to avoid ghost updated:

    HasMany(x => x.ProjectsTags)
        .Access.CamelCaseField()
        .AsBag().Inverse().Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(80);
    

    Anyway…

    Your association seems to be diabolically complex. If ProjectsTags table should contains only id of tag and id of project, then it would be simpler to use FNH many-to-many bidirectional mapping:

    public class Tag2Map : ClassMap<Tag2>
    {
        public Tag2Map()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasManyToMany(x => x.Projects)
                .AsBag()
                .Cascade.None()
                .Table("ProjectsTags")
                .ParentKeyColumn("TagId")
                .ChildKeyColumn("ProjectId");
        }
    }
    
    public class Project2Map : ClassMap<Project2>
    {
        public Project2Map()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasManyToMany(x => x.Tags)
                .AsBag()
                .Cascade.None()
                .Inverse()
                .Table("ProjectsTags")
                .ParentKeyColumn("ProjectId")
                .ChildKeyColumn("TagId");
        }
    }
    

    Now there is no need for ProjectTag entity in the model. The count of how many times is given tag used can be retrieved in two ways:

    Direct way: tag.Projects.Count() – but it retrieves all projects from database.

    Query way:

    var tag = session.Get<Tag2>(tagId);
    var count = session.Query<Project2>().Where(x => x.Tags.Contains(tag)).Count();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with 3 tables in it. They are Projects, Contacts and
OK, I have 3 database tables at the moment: users id: 1, 2 name:
I am designing a database for a project. I have a table that has
I Have a DataBase in my project With Table named 'ProcessData' and columns named
I have four tables in database. person(Perid ,firstname , lastname , gender) Research (Resid
I have two database tables with the following structure: actions: action_id int(11) primary key
I have 3 database tables, all of them have the same 5 columns. They
I have several database tables that just contain a single column and very few
I have three database tables: users emails invitations Emails are linked to users by
I have five tables in my database. Members, items, comments, votes and countries. I

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.