插入后 NHibernate 仍会发出更新 [英] NHibernate still issues update after insert
问题描述
我有一个非常简单的单向映射.见下文:
I have a very simple unidirectional mappings. see below:
public ContactMap()
{
Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Name);
References(x => x.Device);
HasMany(x => x.Numbers)
.Not.Inverse()
.Not.KeyNullable()
.Cascade.AllDeleteOrphan()
.Not.LazyLoad()
.Fetch.Subselect();
Table("Contacts");
}
public PhoneNumberMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Number);
Table("ContactNumbers");
}
根据此 post 在 nhibernate 3 及更高版本之后,将键设置为不可为空应该可以解决插入更新问题(当 NHibernate 发出外键设置为 null 的插入,然后进行更新以将外键更新为正确值时的问题),但对我而言并非如此.当我将键设置为不可为空时,NHibernate 发出正确的插入语句
According to this post after nhibernate 3 and above, setting key as non-nullable should fix the insert-update issue (The issue when NHibernate issues an insert with foreign key set to null and then an update to update the foreign key to correct value), but this is not the case for me. When I set the key as not nullable, NHibernate issues a correct insert statement
INSERT INTO ContactNumbers
(Number,
ContactId)
VALUES ('(212) 121-212' /* @p0 */,
10 /* @p1 */);
如你所见,它插入了 ContactId 字段,但之后它仍然发出更新语句
As you can see, it inserts ContactId field, but after that, it still issues update statement
UPDATE ContactNumbers
SET ContactId = 10 /* @p0 */
WHERE Id = 34 /* @p1 */
所以要澄清问题.NHibernate 插入具有正确分配的外键的 Contact 行,然后发出更新语句来更新冗余的外键 (ContactId).
So to clarify the problem. NHibernate inserts Contact row with foreign key assigned correctly and after that, it issues an update statement to update the foreign key (ContactId) which is redundant.
我怎样才能去掉这个多余的更新语句?谢谢.
How can I get rid of this redundant update statement? Thanks.
顺便说一句,我使用的是最新版本的 NHibernate 和 Fluent NHibernate.数据库是SQLite
BTW, I'm using latest version of NHibernate and Fluent NHibernate. The database is SQLite
推荐答案
您必须将 "updatable"=false
设置为您的密钥以防止更新.
You have to set "updatable"=false
to your key to prevent update.
public ContactMap()
{
Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Name);
References(x => x.Device);
HasMany(x => x.Numbers)
.Not.Inverse()
.Not.KeyNullable()
.Not.KeyUpdate() // HERE IT IS
.Cascade.AllDeleteOrphan()
.Not.LazyLoad()
.Fetch.Subselect();
Table("Contacts");
}
这篇关于插入后 NHibernate 仍会发出更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!