开发者

Fluent Nhibernate ManyToMany collection - not saving associations

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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜