NHibernate插入后仍然会发布更新 [英] NHibernate still issues update after insert
问题描述
$ p $
public ContactMap()
{
Id(x => x.Id) .GeneratedBy.Assigned();
Map(x => x.Name);
引用(x => x.Device);
HasMany(x => x.Numbers)
.Not.Inverse()
.Not.KeyNullable()
.Cascade.AllDeleteOrphan()
.Not .LazyLoad()
.Fetch.Subselect();
表(联系人);
$ b public PhoneNumberMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Number);
表(ContactNumbers);
INSERT INTO ContactNumbers
(Number,
ContactId)
VALUES('(212)121-212'/ * @ p0 * /,
10 / * @ p1 * /);
正如您所见,它会插入ContactId字段,但在此之后, p>
UPDATE ContactNumbers
SET ContactId = 10 / * @ p0 * /
WHERE Id = 34 / * @ p1 * /
所以澄清问题。 NHibernate插入正确分配的外键的联系行,然后发出更新语句来更新冗余的外键(ContactId)。
如何摆脱这个多余的更新声明?
Thanks。
顺便说一下,我正在使用最新版本的NHibernate和Fluent NHibernate。数据库是SQLite
您必须设置updatable= false
到您的密钥,以防止更新。
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()//这里是
.Cascade.AllDeleteOrphan()
.Not.LazyLoad()
.Fetch.Subselect();
表(联系人);
}
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");
}
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 */);
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 */
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.
BTW, I'm using latest version of NHibernate and Fluent NHibernate. The database is SQLite
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屋!