我并不完全是一名NHibernate专家,因此这可能是该部门缺乏了解。我有两个具有多对多关系的简单实体
public class Category
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual bool Visible { get; set; }
public virtual IList<Product> Products { get; set; }
}
public class Product
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual decimal Price { get; set; }
public virtual bool Visible { get; set; }
public virtual IList<Category> Categories { get; set; }
}
我的配置是
public static void BuildFactory()
{
_factory = Fluently.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString(_connectionString)
.ShowSql()
)
.Mappings(m =>
m.AutoMappings.Add(
AutoMap.AssemblyOf<Database>()
.Where(t => t.Namespace == "FancyStore.Models.Catalog")
.Override<Product>(k =>
k.HasManyToMany(x => x.Categories)
.Table("ProductsToCategories")
.ParentKeyColumn("Product_id")
.ChildKeyColumn("Category_id")
.Cascade.AllDeleteOrphan())
.Override<Category>(k =>
k.HasManyToMany(x => x.Products)
.Table("ProductsToCategories")
.ParentKeyColumn("Category_id")
.ChildKeyColumn("Product_id")
.Cascade.AllDeleteOrphan().Inverse())
)
)
.ExposeConfiguration(SetConfiguration)
.BuildConfiguration()
.BuildSessionFactory();
}
因此,我开始时没有覆盖(后来在谷歌搜索后添加了覆盖),并使用ExportSchema生成模式。ExportSchema知道多对多关系(即使没有覆盖),因为它生成了一个联接表(
ProductsToCategories
).
我想添加一些简单的测试数据,所以我这样做了
using (var db = new Repository<Category>())
{
for (int i = 0; i < 6; i++)
{
var cat = new Category
{
Name = "Things" + i,
Description = "Things that are things",
Visible = true,
Products = new List<Product>()
};
for (int k = 0; k < 6; k++)
{
var prod = new Product
{
Name = "Product" + k,
Description = "Product for " + cat.Name,
Visible = true,
Categories = new List<Category>(new[] { cat })
};
cat.Products.Add(prod);
}
db.Save(cat);
}
}
这会正确保存产品和类别,但不会保存联接表中的任何关系。查看调用category.Products生成的sql语句,它实际上是在该联接表中查找产品(并且找不到任何产品,因为它是空的)
任何帮助都将不胜感激:-)
:已删除反向,问题仍在发生:(
:
以下是目录和产品的映射文件(注意:名称有点傻,但这只是一个简单的交易)
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="FancyStore.Models.Catalog.Product, FancyStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Product`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
<property name="Description" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Description" />
</property>
<property name="Price" type="System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Price" />
</property>
<property name="Visible" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Visible" />
</property>
<bag cascade="all-delete-orphan" inverse="true" name="Categories" table="ProductsToCategories">
<key>
<column name="Product_id" />
</key>
<many-to-many class="FancyStore.Models.Catalog.Category, FancyStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Category_id" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="FancyStore.Models.Catalog.Category, FancyStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Category`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
<property name="Description" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Description" />
</property>
<property name="Visible" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Visible" />
</property>
<bag cascade="all-delete-orphan" name="Products" table="ProductsToCategories">
<key>
<column name="Category_id" />
</key>
<many-to-many class="FancyStore.Models.Catalog.Product, FancyStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Product_id" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>