S#arp Architecture many-to-many mapping overrides not working

巧了我就是萌 提交于 2019-12-11 01:48:06

问题


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:


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.



来源:https://stackoverflow.com/questions/1816342/sarp-architecture-many-to-many-mapping-overrides-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!