这是我的应用程序设置: Web客户端< - > 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调试模式时,值 IS 都会更新,并手动点击从以下代码行返回的IEnumerable上的“展开结果”:
var attrib = tempAttribute.Options.Where(e => e.Id == parsedId).Select(e => e.Name = model.EditRequest.Name);
但是,如果我没有手动“扩展结果”(就像我在下面的图像上所做的那样),它似乎也不会更新数据库。
a busy cat http://i54.tinypic.com/311ungn.gif
这怎么可能?这对我来说没什么意义..
非常感谢任何帮助或理论! : - )
非常感谢!
答案 0 :(得分:1)
原因是attrib实际上并不包含项目列表。它包含一个迭代器。因此,除非您枚举attrib,否则Select部分不会自动执行。扩展结果时,您正在执行的操作是什么。我建议手动进行设置,以便更明显地发生了什么,或者考虑使用Automapper或ValueInjecter等工具。