问题描述
我已经尝试了几乎所有的东西来获得在S#arp Architecture中工作的M:M映射。不幸的是,Northwind示例项目没有M:M覆盖。在转换为S#arp和选择Fluent NHibernate自动映射之前,我喜欢自动映射,这是很好的,但重写似乎并没有注册。
这似乎都工作在内存和测试,但是当提交数据一个数据库没有任何东西插入到我的M:M参考表中。如果我们拿一个类别的简单样本可以有很多产品,而一个产品可以有很多类别,将有一个名为CategoryProduct(我不喜欢pluralisation),有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();
$ b $产品有一个映射覆盖如下: / p>
public class ProductMap:IAutoMappingOverride< Product>
public void Override(AutoMap< Product>映射)
{
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();
$ b我已经尝试了很多结构化M: M映射,但没有任何作品。
这个建议使用更新FHN重新编译S#arp,但我试过这个,但是最新的FHN代码与S#arp使用的代码差别很大。修正了所有中断冲突,但它仍然无法正常工作。
希望有人遇到并解决了S#arp的M:M自动映射覆盖问题。管理解决了这个问题,竟然是一个S#arp初学者的错误。
$ b 解决方案 $ b 对于要保存的ManyToMany数据,控制器方法需要为其分配[Transaction]属性。
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.
解决方案 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.
这篇关于S#arp体系结构多对多映射覆盖不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-06 16:48