gpt4 book ai didi

nhibernate - 在 NHibernate 中映射 View 的策略

转载 作者:行者123 更新时间:2023-12-04 08:00:09 25 4
gpt4 key购买 nike

NHibernate 似乎需要将 id 标记指定为映射的一部分。这给 View 带来了问题,因为大多数时候(根据我的经验) View 没有 Id。我之前在 nhibernate 中映射过 View ,但是我这样做的方式对我来说似乎很困惑。

这是一个 人为 我目前如何做的例子。

制图

  <class name="ProductView" table="viewProduct" mutable="false" >
<id name="Id" type="Guid" >
<generator class="guid.comb" />
</id>
<property name="Name" />
<!-- more properties -->
</class>

查看 SQL
Select NewID() as Id, ProductName as Name, --More columns
From Product

类(class)
public class ProductView
{
public virtual Id {get; set;}
public virtual Name {get; set;}
}

我不需要产品的 ID,或者在某些 View 的情况下,我可能没有 View 的 ID,具体取决于我是否可以控制 View

有没有更好的方法将 View 映射到 nhibernate 中的对象?

编辑
到目前为止的答案

制图
  <class name="ProductView" table="viewProduct" mutable="false" >
<id name="Id" type="Guid" />
<property name="Name" />
<!-- more properties -->
</class>

类(class)
 public class ProductView
{
public virtual Name {get; set;}
//more properties
}

查看 SQL
我还需要 NewID() 吗?
Select NewID() as Id, ProductName as Name, --More columns
From Product

最佳答案

您可以通过不将 Id 映射到属性并省略生成器来使其更简洁:

<id column="Id" type="guid"/>

这样,您可以将问题保留在数据层中,而不会将实现细节泄漏到您的域中。

关于nhibernate - 在 NHibernate 中映射 View 的策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2963020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com