我有这个问题。我有这样的课:
public class WfStep
{
private readonly IDictionary properties = new Hashtable();
public virtual Guid Id { get; private set; }
public virtual string Name { get; set; }
public virtual dynamic Properties { get { return new HashtableDynamicObject(properties); } }
及其映射文件如下:
<class name="ConsoleApplication4.WfStep" table="WFSteps">
<id name="Id">
<generator class="guid"/>
</id>
<property name="Name" />
<map name="Properties" table="WFSteps_Properties" access="field.lowercase">
<key column="StepId" />
<index column="PropertyName" type="System.String"/>
<composite-element class="ConsoleApplication4.ValueItem, ConsoleApplication4">
<property name="Value" type="System.String"/>
<property name="ValueType" type="System.String"/>
</composite-element>
</map>
此示例移植自: Ayende Support dynamic fields with NHibernate and .NET 4.0
然后我像这样将一个对象保存到数据库中:即。
var step = new WfStep { Name = "John" };
var property = new ValueItem() { Value = DateTime.Now, ValueType = "System.DateTime" };
step.Properties["DateProperty"] = property ;
Session.Save(step);
现在我想返回所有将 DateProperty 设置为 2010 年的 WFStep,即:
var Session.Query<WfStep>().Where(x=>x.Properties.DateProperty.Year == 2010);
它抛出一个错误:
表达式树可能不包含动态操作
如何查询这种具有动态属性的类。?