2

我已经尝试了几乎所有可以在S#arp体系结构中工作的M:M映射。不幸的是,Northwind示例项目没有M:M覆盖。S#arp架构多对多映射覆盖不起作用

在转换为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映射的组合,但没有任何工作。

这个article建议用更新FHN重新编译S#arp,我试过这个,但是最新的FHN代码与S#arp使用的代码差别很大。修复了所有中断冲突,但仍然无效。

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

+0

是否执行覆盖(如果您在覆盖方法中插入断点 - 那些断点命中?)?你可以发布节省产品/类别的代码吗? – maciejkow 2009-12-01 21:57:11

回答

1

管理解决了这个问题,竟然是一个S#arp初学者的错误。

对于要保存的ManyToMany数据,控制器方法需要为其分配[T​​ransaction]属性。