NHibernate 不更新实体的所有属性
我在使用 FluentNHibernate 时遇到了一个奇怪的问题:当我保存实体时,(参考)属性之一未更新。其他属性(字段和引用)都会更新,并且失败的属性会被正确映射(检索实体就像一个魅力)。
我正在做的事情的(稍微简化的)描述:
- 在我的 MVC 操作方法中,绑定并设置了一个
InputModel
。它有一个TypeID
属性,我希望在其中设置实体的Type
(我们将其称为实体类型Thing
)。 - 将创建一个新的
Thing
对象,并复制InputModel
的简单属性。对于几个复杂的属性,其中Type
属性不起作用,另一个属性则完成以下操作:
2.1.根据提供的类型 ID 从存储库中获取正确的ThingType
。
2.2.在新的Thing
对象上设置类型(使用thing.Type = theType
)。 - 我想要更新的
Thing
是根据输入模型上的 ID(与TypeID
不同的 ID)从存储库中获取的。 - 所有属性,无论是复杂的还是其他的,都会从新事物(由我创建)复制到原始事物(从数据库获取)。
- 使用
session.Save();
保存原始的Thing
。
如上所述,只有一个属性不起作用 - 其他属性遵循(据我所知)完全相同的模式,起作用。我还调试并验证了原始 Thing
在传递给 session.Save()
时是否具有正确的、更新的 Type
。
我不知道从哪里开始解决这个问题...
更新:这些类是普通的 POCO:
public class Thing
{
public int ID { get; set; }
public string SomeSimpleProp { get; set; }
public ThingType Type { get; set; }
public OtherEntity OtherReference { get; set; }
}
public class ThingType
{
public int ID { get; set; }
public string Name { get; set; }
}
我的确切映射(类型和属性的名称除外)是这些:
// In ThingMap : ClassMap<Thing> constructor:
Id(t => t.ID).Column("ThingID");
Map(t => t.SomeSimpleProp);
References(t => t.Type).Column("ThingTypeID");
References(t => t.OtherReference).Column("OtherReferenceID");
// In ThingTypeMap : ClassMap<ThingType> constructor:
Id(t => t.ID).Column("ThingTypeID");
Map(t => t.Name);
正如我所说,OtherReference
已正确更新,而 Type
未正确更新。它们的映射相同,所以我不明白这怎么可能是映射错误。
I'm experiencing an odd problem with FluentNHibernate: when I save my entity, one of the (reference) properties is not updated. Other properties, both fields and references, are updated, and the failing property is correctly mapped (retrieving entities works like a charm).
A (slightly simplified) description of what I'm doing:
- Into my MVC action method, an
InputModel
is bound and set. It has a property for theTypeID
, where I wish to set theType
of my entity (let's call the entity typeThing
). - A new
Thing
object is created, and the simple properties of theInputModel
is copied over. For a couple of complex properties, among them theType
property which isn't working and another property which is, the following is done:
2.1. The correctThingType
is fetched from the repository, based on the provided type id.
2.2. The type is set (usingthing.Type = theType
) on the newThing
object. - The
Thing
that I want to update is fetched from the repository, based on the id on the input model (not the same id as theTypeID
). - All properties, complex and other, are copied over from the new thing (created by me) to the original one (fetched from db).
- The original
Thing
is saved, usingsession.Save();
.
As stated above, it's only one property that isn't working - other properties, following (as far as I can tell) the exact same pattern, work. I've also debugged and verified that the original Thing
has the correct, updated Type
when it is passed to session.Save()
.
I have no idea where to start troubleshooting this...
Update: The classes are plain POCOs:
public class Thing
{
public int ID { get; set; }
public string SomeSimpleProp { get; set; }
public ThingType Type { get; set; }
public OtherEntity OtherReference { get; set; }
}
public class ThingType
{
public int ID { get; set; }
public string Name { get; set; }
}
My exact mappings (except for the names of types and properties) are these:
// In ThingMap : ClassMap<Thing> constructor:
Id(t => t.ID).Column("ThingID");
Map(t => t.SomeSimpleProp);
References(t => t.Type).Column("ThingTypeID");
References(t => t.OtherReference).Column("OtherReferenceID");
// In ThingTypeMap : ClassMap<ThingType> constructor:
Id(t => t.ID).Column("ThingTypeID");
Map(t => t.Name);
As I said, OtherReference
is updated correctly while Type
is not. They are mapped identically, so I don't see how this could be a mapping error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该指定
以便更新引用。You should specify
<many-to-one .... cascade="save-update"/>
in order to update references.