Nhibernate fluent ignore 属性 while updating with condition
Nhibernate fluent ignore property while updating with condition
如何使用 NHibernate 更新实体并忽略其某些属性,以防它们的值为空(在更新的实体中)?
如果 属性 值不为空,则应将其包含在更新中。
示例,实体具有 Id
和 Name
:
- 如果名称为空,我们将更新没有名称的实体。
- 如果名称不为空,我们将使用该名称更新实体。
你需要做两件事:
- 启用dynamic updates。默认情况下,NHibernate 为每个实体准备一个更新查询,更新除主键之外的所有列。在更新实体时,它通过指定所有值(包括未更改的值)来使用它。因此,您需要告诉 NHibernate 使用动态更新,它会在每次更新时生成一个临时更新查询,以仅更新已更改的属性。
使用 hbm,您必须将 dynamic-update="true"
放在 class 映射上。可能有一些足够的 class 映射方法来流畅地调用它。
- 使用 interceptor or an event for customizing the dirtiness check algorithm. There are a bunch of answers about that on Stack Overflow, like this one and this other one (from me).
它们与您的要求无关,但适应它们应该不难。
如何使用 NHibernate 更新实体并忽略其某些属性,以防它们的值为空(在更新的实体中)?
如果 属性 值不为空,则应将其包含在更新中。
示例,实体具有 Id
和 Name
:
- 如果名称为空,我们将更新没有名称的实体。
- 如果名称不为空,我们将使用该名称更新实体。
你需要做两件事:
- 启用dynamic updates。默认情况下,NHibernate 为每个实体准备一个更新查询,更新除主键之外的所有列。在更新实体时,它通过指定所有值(包括未更改的值)来使用它。因此,您需要告诉 NHibernate 使用动态更新,它会在每次更新时生成一个临时更新查询,以仅更新已更改的属性。
使用 hbm,您必须将dynamic-update="true"
放在 class 映射上。可能有一些足够的 class 映射方法来流畅地调用它。 - 使用 interceptor or an event for customizing the dirtiness check algorithm. There are a bunch of answers about that on Stack Overflow, like this one and this other one (from me).
它们与您的要求无关,但适应它们应该不难。