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

The Archive Base Latest Questions

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

I can’t get my head around why this isn’t working.. I have a relatively

  • 0

I can’t get my head around why this isn’t working..

I have a relatively clean entity model consisting of POCOs created with DDD in mind (while probably not following most rules even loosely).

I am using Fluent NHibernate to do the mapping. I am also using SchemaExport to create the database schema, with minimum input from me on how to do it. NHibernate is free to choose the best way.

I have two entities with Many-to-many relationships with each other (non-interesting code removed); MediaItem and Tag; MediaItems can have many tags, Tags can be applied to many MediaItems, and I want collections on both sides so I can easily get at stuff.

(A bit of a formatting issue below, sorry)

  • MediaItem:

    public class MediaItem
    {
    
    private IList<Tag> _tags;
    
    public virtual long Id { get; set; }
    
    public virtual string Title { get; set; }
    
    public virtual IEnumerable<Tag> Tags { get { return _tags; } }
    
    public MediaItem()
    {
        _tags = new List<Tag>();
    }
    
    public virtual void AddTag(Tag newTag)
    {
        _tags.Add(newTag);
        newTag.AddMediaItem(this);
    }
    

    }

  • Tag:

    public class Tag
    {

    private IList<MediaItem> _mediaItems;
    public virtual long Id { get; set; }
    public virtual string TagName { get; set; }
    public virtual IEnumerable<MediaItem> MediaItems { get { return _mediaItems; } }
    
    public Tag()
    {
        _mediaItems = new List<MediaItem>();
    }
    
    protected internal virtual void AddMediaItem(MediaItem newItem)
    {
        _mediaItems.Add(newItem);
    }
    

    }

I have tried to be smart about only exposing the collections as IEnumerable, and only allowing adding items through the methods. I also hear that only one side of the relationship should be responsible for this – thus the contrived AddMediaItem() on Tag.

The MediaItemMap looks like this:

public class MediaItemMap : ClassMap<MediaItem>
{
    public MediaItemMap()
    {
        Table("MediaItem");

        Id(mi => mi.Id);

        Map(mi => mi.Title);

        HasManyToMany<Tag>(mi => mi.Tags)
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.SaveUpdate();
    }
}

The Tag mapping looks like this:

public class TagMap : ClassMap<Tag>
{
    public TagMap()
    {
        Table("Tag");

        Id(t => t.Id);

        Map(t => t.TagName);

        HasManyToMany<MediaItem>(mi => mi.MediaItems)
            .Access.CamelCaseField(Prefix.Underscore)
            .Inverse();
    }
}

Now I have some test code that drops the database schema, recreates it (since I am shotgun debugging my brains out here), and then runs the following simple code:

Tag t = new Tag { TagName = "TestTag" };
MediaItem mi = new MediaItem { Title = "TestMediaItem" };

mi.AddTag(t);

var session = _sessionFactory.OpenSession();

session.Save(mi);

Yep, this is test code, it will never live past the problem in this post.

The MediaItem is saved, and so is the Tag. However, the association between them is not. NHibernate does create the association table “MediaItemsToTags”, but it doesn’t attempt to insert anything into it.

When creating the ISessionFactory, I specify ShowSQL() – so I can see all the DDL sent to the SQL server. I can see the insert statement for both the MediaItem and the Tag tables, but there is no insert for MediaItemsToTags.

I have experimented with many different versions of this, but I can’t seem to crack it. Cascading is one possible problem, I’ve tried with Cascade.All() on both sides, Inverse() on both sides etc., but no dice.

Can anyone tell me what is the correct way to map this to get NHibernate to actually store the association whenever I store my MediaItem?

Thanks!

  • 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-14T04:35:25+00:00Added an answer on May 14, 2026 at 4:35 am

    You need to define the many-to-many table and parent and child key columns:

    public class MediaItemMap : ClassMap<MediaItem>
    {
        public MediaItemMap()
        {
            Table("MediaItem");
    
            Id(mi => mi.Id);
    
            Map(mi => mi.Title);
    
            HasManyToMany<Tag>(mi => mi.Tags)
                .Table("MediaItemsToTags").ParentKeyColumn("Id").ChildKeyColumn("Id")
                .Access.CamelCaseField(Prefix.Underscore)
                .Cascade.SaveUpdate();
        }
    }
    

    The syntax is identical in TagMap because both key columns are named “Id”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I get a 'when to use' for these and others? <% %> <%#
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can anyone tell me how I can display a status message like 12 seconds
Can you tell me what is the difference between abstraction and information hiding in

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.