gpt4 book ai didi

c# - Fluent NHibernate(带自动映射)不以多对多方式保存连接表值

转载 作者:太空狗 更新时间:2023-10-30 01:27:53 25 4
gpt4 key购买 nike

我不完全是 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,它实际上是在该连接表中查找产品(并且找不到任何产品,因为它是空的)

任何帮助将不胜感激:-)

编辑:删除了一个逆,问题仍然存在:(

EDIT2:

这是目录和产品的映射文件(注意:这个名字有点傻,但这只是一种快速的原型(prototype)交易)

<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>

最佳答案

我可能弄错了,但看起来您将双方的关系标记为“反向”。

您应该只对关系的一侧执行此操作,并确保在另一侧调用 Save

此外,我相信在保存之前,这种关系需要在双方都有效(添加 catprod.Categories,除了你已经在做的).

诚然,我对这些关系的理解是模糊的,但这是我几个月来的研究得出的结论(并解决了我自己的一些问题)。

关于c# - Fluent NHibernate(带自动映射)不以多对多方式保存连接表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2028905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com