gpt4 book ai didi

c# - NHibernate 重新创建每个关联项

转载 作者:太空狗 更新时间:2023-10-30 00:43:22 24 4
gpt4 key购买 nike

我有一个非常简单的对象模型。

public class Contact
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Device Device { get; set; }
public virtual IList<string> Numbers { get; set; }

public Contact()
{
Numbers = new System.Collections.Generic.List<string>(3);
}
}

如您所见,类 Contact 与 Numbers 有关联,它是一个字符串列表。

这是映射:

        Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Name);
References(x => x.Device, "DeviceId");
Table("Contacts");
HasMany(x => x.Numbers)
.Table("ContactNumbers")
.Element("Number")
.KeyColumn("ContactId")
.LazyLoad()
.Cascade.All()
.Not
.Inverse();

请注意,我不能也不希望集合为 inverse=true,因为它只是一个字符串集合。这意味着 Contact 负责更新 Numbers 条目。现在我的问题是,每当我尝试向现有联系人添加新号码时,它都会删除所有关联的号码并单独重新创建它们。 NHibernate 不够智能,无法检测更改并仅更新更改的项目吗?

我认为我的问题应该有一个简单的解决方案,但不知道是什么。

如有任何帮助,我们将不胜感激。

最佳答案

这实际上是documented在 NHibernate 的文档中。

Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. NHibernate has no way of distinguishing between duplicate rows. NHibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient.

尝试使用 <idbag> 映射,并为该表创建代理主键。不幸的是,看起来像 <idbag>not yet supportedFluentNHibernate .

另外,看看其他 collection mapping options .

关于c# - NHibernate 重新创建每个关联项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11415991/

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