gpt4 book ai didi

c# - NHibernate(Fluent NHibernate)中的多对多关系在数据库中创建意外记录

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:52 25 4
gpt4 key购买 nike

我正在使用 Fluent NHibernate 处理 Store 和 Product 之间的多对多关系。我没有发布实体,因为它无关紧要。这些是我的映射

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using FluentNHibernateSample.Domain;

namespace FluentNHibernateSample.Mappings
{
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Price);
HasManyToMany(x => x.Stores).Cascade.All().
Table("StoreProduct").ParentKeyColumn("ProductId")
.ChildKeyColumn("StoreId");
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using FluentNHibernateSample.Domain;

namespace FluentNHibernateSample.Mappings
{
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Staff).KeyColumn("StoreId").Cascade.All();
HasManyToMany(x => x.Products).Cascade.All().
Table("StoreProduct").
ParentKeyColumn("StoreId").ChildKeyColumn("ProductId");
}
}
}

这是我尝试在数据库中插入记录的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernateSample.Domain;

namespace FluentNHibernateSample
{
class Program
{
static void Main(string[] args)
{
ISessionFactory sessionFactory = CreateSessionFactory();

using (var session = sessionFactory.OpenSession())
{
session.Transaction.Begin();

Store store = new Store();
store.Name = "Emall";

Product product = new Product();
product.Name = "Emall Item 1";
product.Price = 12.5;

//Sample code below

session.Save(store);

session.Transaction.Commit();

Console.ReadKey();
}

}

private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure().
Database(MsSqlConfiguration.MsSql2008.
ConnectionString( m => m.FromConnectionStringWithKey(System.Environment.MachineName))).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>()).
BuildSessionFactory();
}
}
}

示例代码:

            //option 1 ==> doesn't work
product.Stores.Add(store);

//option 2 ==> works
store.Products.Add(product);

//option 3 ==> creates 2 entries in Junction table
product.Stores.Add(store);
store.Products.Add(product);

为什么第二个选项有效而第一个选项无效?我知道这是因为我正在将商店添加到产品并保存商店。但是第一个选项不应该因为对象跟踪而起作用吗?此外,第三个选项在联结表中创建了 2 条记录,这让我大吃一惊。我该如何解决这个问题?映射类有什么问题吗?

最佳答案

双向 manytomany 的一侧必须是负责关联的一侧,这意味着负责插入链接记录。不负责的一方必须说 .Inverse() 来告诉 NH 另一方负责。

// for example
HasManyToMany(x => x.Products).Inverse()

更新:为了保持一致,您始终需要使用选项 3,否则它会连接到数据库而不是内存中,这会导致细微的错误

// in Store
public virtual void Add(Product product)
{
if(!Products.Contains(product))
{
Products.Add(product);
product.Add(this);
}
}

// in Product
public virtual void Add(Store store)
{
if(!Stores.Contains(store))
{
Stores.Add(store);
store.Add(this);
}
}

关于c# - NHibernate(Fluent NHibernate)中的多对多关系在数据库中创建意外记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346886/

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