views:

201

answers:

1

I have tried pretty much everything to get M:M mappings working in S#arp Architecture. Unfortunately the Northwind example project does not have a M:M override.

All worked fine in my project before converting to S#arp and its choice of Fluent NHibernate's Auto mapping. I like the auto-mapping, it's good, however the overrides do not seem to register.

It all seems to work in memory and in tests, however when committing data to a database nothing gets inserted into my M:M reference table.

If we take the simple sample of a Category can have many Products and a Product can be in many Categories we would have a table called CategoryProduct (I don't like pluralisation) that has columns Category_id and Product_id.

My Auto persistence model generates as such:

return AutoPersistenceModel
    .MapEntitiesFromAssemblyOf<Category>()
    .Where(GetAutoMappingFilter)
    .ConventionDiscovery.Setup(GetConventions())
    .WithSetup(GetSetup())
    .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

Mapping override for Category looks like such:

public class CategoryMap : IAutoMappingOverride<Category>
{
    public void Override(AutoMap<Category> mapping)
    {
        mapping.Id(x => x.Id, "Id")
            .WithUnsavedValue(0)
            .GeneratedBy.Identity();

        mapping.Map(x => x.Name).WithLengthOf(50);

        mapping.Map(x => x.Depth);

        mapping.HasMany<Category>(x => x.Children)
            .Cascade.All()
            .KeyColumnNames.Add("Parent_id")
            .AsBag()
            .LazyLoad();

        mapping.HasManyToMany<Posting>(x => x.Products)
            .WithTableName("CategoryProduct")
            .WithParentKeyColumn("Category_id")
            .WithChildKeyColumn("Product_id")
            .Cascade.All()
            .AsBag();
    }
}

And the Product has a mapping override as such:

public class ProductMap : IAutoMappingOverride<Product>
{
    public void Override(AutoMap<Product> mapping)
    {
        mapping.Id(x => x.Id, "Id")
            .WithUnsavedValue(0)
            .GeneratedBy.Identity();

        mapping.Map(x => x.Title).WithLengthOf(100);
        mapping.Map(x => x.Price);
        mapping.Map(x => x.Description).CustomSqlTypeIs("Text");
        mapping.References(x => x.Category).Cascade.All();

        mapping.HasMany<ProductImage>(x => x.Images).Inverse().Cascade.All().LazyLoad();

        mapping.HasManyToMany<Category>(x => x.Categories)
            .WithTableName("CategoryProduct")
            .WithParentKeyColumn("Product_id")
            .WithChildKeyColumn("Category_id")
            .Inverse()
            .AsBag();
    }
}

I've tried many combinations of structuring the M:M mappings, but nothing works.

This article has suggestion to re-compile S#arp with update FHN, I tried this however the latest FHN code is vastly different to that used by S#arp it would seem. Fixed all the breaking conflicts but it still doesn't work.

Hopefully someone else has encountered and resolved M:M auto-mapping override problems with S#arp.

+1  A: 

Managed to solve the issue, turned out to being a S#arp beginners error.

For ManyToMany data to be saved then the controller method need to have the [Transaction] attribute assigned to it.

Jordan