0

我有以下 C# 类(简化):

class Entity {
    Guid Id { get; set;}
    ISet<Uri> Urls { get; set; }
}

我想将其映射到以下表结构:

TABLE Entity (
   Id uniqueidentifier PRIMARY KEY       
)

TABLE EntityUrls (
   Id uniqueidentifier PRIMARY KEY,
   EntityId uniqueidentifier FOREIGN KEY REFERENCES(Entity.Id),
   Uri varchar(250)
)

我主要可以使用它来映射它<set>,比如

<set name="Urls" table="EntityUrls">
  <key column="EntityId"/>
  <element column="Uri" type="..."/>
</set>

但是我如何映射EntityUrls.Id列(至少生成 Guid on INSERT)?
或者在这种情况下通常建议使用复合PK?

4

1 回答 1

1

您可以通过创建自定义用户类型来尝试一下ICompositeUserType

<set name="Urls" table="EntityUrls">
  <key column="EntityId"/>

  <element type="UserTypes.UriCompositeUserType">
    <column name="Id" />
    <column name="Uri" />
  </element>
</set>

这是 NHibernate 所说的ICompositeUserType

A UserType that may be dereferenced in a query. This interface allows a custom type to define "properties". These need not necessarily correspond to physical .NET style properties. A ICompositeUserType may be used in almost every way that a component may be used. It may even contain many-to-one associations. Implementors must be immutable and must declare a public default constructor. Unlike UserType, cacheability does not depend upon serializability. Instead, Assemble() and Disassemble() provide conversion to/from a cacheable representation.

于 2013-11-14T00:50:35.943 回答