views:

117

answers:

1

I have a Resource entity which has a many-to-many relationship with a File entity, such that a resource can reference multiple files and a file can reference multiple resources.

Now I expect the save to cascade down from the Resource to the File entities when the Resource is Saved - When I load a Resource entity it is loading the File entities as expected. It's not saving the Resource entity references to the File.

Does anyone know what's wrong with the FNH mapping?

The database tables with primary keys are:

Resources - Id

ResourceFiles - ResourceId, FileId

Files - Id

The FNH mapping code is:

public sealed class ResourceMap : ClassMap<Resource>, IMap
{
    public ResourceMap()
    {
        Table("Resources");
        Not.LazyLoad();
        Id(x => x.Id);
        HasManyToMany<File>(x => x.Files)
            .AsBag()
            .Inverse()
            .Cascade.All()
            .ParentKeyColumn("ResourceId")
            .ChildKeyColumn("FileId")
            .Table("ResourceFiles");
    }
}

public sealed class FileMap : ClassMap<File>, IMap
    {
        public FileMap()
        {
            Table("Files");
            Not.LazyLoad();
            Id(x => x.Id);
            HasManyToMany<Resource>(x => x.Resources)
                .Not.LazyLoad()
                .AsBag()
                .Cascade.All()
                .ParentKeyColumn("FileId")
                .ChildKeyColumn("ResourceId")
                .Table("ResourceFiles");
        }
    }

Cheers

Awkward

+1  A: 

In your Resource class map, you have Inverse() specified, which means that the Resource class expects the File class to do the saving, which is why the changes are not flowing down from Resource to File class. If the resource class is responsible for saving, you need to specify Inverse() on the File mapping class.

Deeksy

related questions