更新 Nhibernate 子属性表现得很奇怪!
这是我的应用程序设置: 网络客户端<-> WCF 服务<->域存储库<-> NHibernate <->数据库
所有项目通过“共享 DLL”共享相同的域实体。
其中一个实体(属性)具有子实体(选项)的集合。映射看起来像这样:
// Attribute mapping
<class name="Attribute" lazy="false" table="Attributes">
<id name="Id" column="AttributeId" type="System.Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Name" type="System.String" length="100" not-null="true" />
<bag name="Options" generic="true" lazy="false" cascade="all-delete-orphan">
<key column="AttributeId" foreign-key="AttributeId" />
<one-to-many class="AttributeOption" not-found="ignore" />
</bag>
</class>
// Attribute option mapping, which is a child of Attribute
<class name="AttributeOption" lazy="false" table="AttributeOptions">
<id name="Id" column="OptionId" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="AttributeId" type="System.Int32" not-null="true" />
<property name="Name" type="System.String" length="100" not-null="true" insert="true" update="true" />
<property name="Order" column="OrderVal" type="System.Int32" not-null="true" insert="true" update="true" />
</class>
一切似乎都工作正常,除了尝试更新 OPTION 属性之一时。
更准确地说;每次我处于 Visual Studio 调试模式并手动单击从以下代码行返回的 IEnumerable 上的“展开结果”时,值都会更新:
var attrib = tempAttribute.Options.Where(e => e.Id == parsedId).Select(e => e.Name = model.EditRequest.Name);
但是,如果我不执行手动“扩展结果”(就像我在下图中所做的那样),它似乎也没有更新数据库。
一只忙碌的猫 http://i54.tinypic.com/311ungn.gif
这是怎么回事可能的?这对我来说没有多大意义。
非常感谢任何帮助或理论! :-)
多谢!
This is my application setup:
Web client <-> WCF Service <-> Domain Repositories <-> NHibernate <-> Database
All projects share the same domain entities, through "shared DLLs".
One of the entities (Attribute) got a collection of child entities (Option). The mapping look like this:
// Attribute mapping
<class name="Attribute" lazy="false" table="Attributes">
<id name="Id" column="AttributeId" type="System.Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Name" type="System.String" length="100" not-null="true" />
<bag name="Options" generic="true" lazy="false" cascade="all-delete-orphan">
<key column="AttributeId" foreign-key="AttributeId" />
<one-to-many class="AttributeOption" not-found="ignore" />
</bag>
</class>
// Attribute option mapping, which is a child of Attribute
<class name="AttributeOption" lazy="false" table="AttributeOptions">
<id name="Id" column="OptionId" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="AttributeId" type="System.Int32" not-null="true" />
<property name="Name" type="System.String" length="100" not-null="true" insert="true" update="true" />
<property name="Order" column="OrderVal" type="System.Int32" not-null="true" insert="true" update="true" />
</class>
Everything seems to work fine, except when trying to update one of the OPTION properties.
To be more precise; The value IS updated every time I'm in Visual Studio debug-mode, and manually click "Expand result" on the IEnumerable returned from the following line of code:
var attrib = tempAttribute.Options.Where(e => e.Id == parsedId).Select(e => e.Name = model.EditRequest.Name);
However, if I don't do a manual "expand result" (like I've done on the image below), it doesn't seem to update the database either.
a busy cat http://i54.tinypic.com/311ungn.gif
How is this even possible? It doesn't make much sense to me..
Any help or theories is very appreciated! :-)
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是 attrib 实际上并不包含项目列表。它包含一个迭代器。因此,除非您枚举 attrib,否则 Select 部分不会自动执行。这就是您扩展结果时所做的事情。我建议手动进行设置,以便更明显地了解正在发生的情况,或者考虑使用 Automapper 或 ValueInjecter 等工具。
The reason is attrib does not actually contain the list of items. It contains an iterator. So the Select part does not get executed automatically unless you enumerate attrib. Which is what you're doing when you expand the results. I would suggest manually doing the setting so that it's a lot more obvious what is happening or consider using a tool like Automapper or ValueInjecter.