S#arp 架构多对多映射覆盖不起作用

发布于 2024-08-13 09:13:13 字数 2620 浏览 9 评论 0原文

我已经尝试了几乎所有方法来让 M:M 映射在 S#arp 架构中工作。不幸的是,Northwind 示例项目没有 M:M 覆盖。

在转换为 S#arp 并选择 Fluent NHibernate 的自动映射之前,我的项目中一切都运行良好。我喜欢自动映射,它很好,但是覆盖似乎没有注册。

这一切似乎在内存和测试中都有效,但是当将数据提交到数据库时,没有任何内容被插入到我的 M:M 参考表中。

如果我们取一个类别可以有许多产品的简单样本,并且一个产品可以在许多类别中,我们将有一个名为 CategoryProduct(我不喜欢复数)的表,其中包含 Category_id 和 Product_id 列。

我的自动持久性模型生成如下:

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

类别的映射覆盖看起来像这样:

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();
    }
}

产品具有这样的映射覆盖:

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();
    }
}

我尝试了多种构造 M:M 映射的组合,但没有任何效果。

这篇文章建议重新编译S# arp 与更新 FHN,我尝试了这个,但是最新的 FHN 代码与 S#arp 使用的代码看起来有很大不同。修复了所有中断冲突,但仍然不起作用。

希望其他人遇到并解决了 S#arp 的 M:M 自动映射覆盖问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-08-20 09:13:13

设法解决了这个问题,结果发现这是一个S#arp初学者错误。

为了保存 ManyToMany 数据,控制器方法需要为其分配 [Transaction] 属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文