加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Nhibernate在select上做更新?

发布时间:2020-12-15 08:19:36 所属栏目:百科 来源:网络整理
导读:我有以下课程: public class Product{ public virtual Guid Id { get; set; } public virtual string Name { get; set; } public virtual Decimal PricePerMonth { get; set; } public virtual BillingInterval DefaultBillingInterval { get; set; } publi
我有以下课程:
public class Product
{
  public virtual Guid Id { get; set; }
  public virtual string Name { get; set; }
  public virtual Decimal PricePerMonth { get; set; }
  public virtual BillingInterval DefaultBillingInterval { get; set; }
  public virtual string AdditionalInfo { get; set; }
}

并且映射看起来像这样:

<class name="Product" table="Products">
    <id name="Id" column="ProductId">
      <generator class="guid.comb"/>
    </id>
    <property name="Name" column="ProductName" not-null="true" type="String" />
    <property name="PricePerMonth" column="PricePerMonth" not-null="true" type="Decimal" />
    <property name="DefaultBillingInterval" type="int" not-null="true" />
    <property name="AdditionalInfo" type="string" not-null="false" />
</class>

我使用Repository< T>使用以下方法的类(Session是返回当前会话的属性):

public IEnumerable<T> FindAll(DetachedCriteria criteria)
{
  return criteria.GetExecutableCriteria(Session).List<T>();
}

现在,当我执行以下操作时(会话与存储库中使用的会话相同):

IEnumerable<ProductDTO> productDTOs = null;
using(ITransaction tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
{
    var products = repository.FindAll(new DetachedCriteria.For<Product>().Add(Restrictions.Like("Name","Some Product%")));
    productDTOs = ToDTOs(products);
    tx.Commit();
}
// Do stuff with DTO's

commit语句就在那里,因为我使用了一个服务层,如果没有错误发生,它会自动提交每个事务.我刚刚在这里折叠了我的服务层,以便于实现可视化.

我的ToDTOs方法只是转换为DTO:

private IEnumerable<ProductDTO> ToDTO(IEnumerable<Product> products)
{
  return products.Select(x => new ProductDTO()
    {
      Id = x.Id,Name = x.Name,PricePerMonth = x.PricePerMonth,AdditionalInfo = x.AdditionalInfo
    }).ToList();
}

我的nhibernate日志显示以下输出:

2010-01-04 19:13:11,140 [4] DEBUG NHibernate.SQL - SELECT ... From Products ...
2010-01-04 19:13:11,237 [4] DEBUG NHibernate.SQL - UPDATE Products ...
2010-01-04 19:13:11,548 [4] DEBUG NHibernate.SQL - UPDATE Products ...
...

因此,通过选择产品,它会为会话提交时返回的每个产品发出更新声明,即使产品中没有任何更改.

有任何想法吗?

解决方法

当我有一个实体没有从属性返回相同的值而不是分配给它的值时,我只有这种效果.然后它被NH视为脏.

例:

class Foo
{
  private string name;

  public string Name 
  { 
    // does not return null when null had been set
    get { return name ?? "No Name"; }
    set { name = value; }
  }

}

这就是我编写映射文件的方法.

<class name="Product" table="Products">
    <id name="Id" column="ProductId">
      <generator class="guid.comb"/>
    </id>
    <property name="Name" column="ProductName" not-null="true" />
    <property name="PricePerMonth" not-null="true" />
    <property name="DefaultBillingInterval" not-null="true" />
    <property name="AdditionalInfo" />
</class>

您不需要指定类型.它们由NHibernate在运行时确定.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读