1

我们使用 nHibernate 的 schemaExport 类创建数据库。我现在有一个带有属性的类,我们从中生成了 nhibernate 映射。这个类的一部分是:

public class PluginInstance
{
    ...
    [Bag(2, Name = "RouteParams", Access = "property", Table = "CMS_PluginInstanceRouteParams")]
    [Key(3, Column = "ParamId")]
    [Element(4, Column = "Param", Type = "string")]
    public virtual IList<String> RouteParams
    {
        get { return _routeParamsField; }
        set { _routeParamsField = value; }
    }
    ...
}

nHibernate映射的生成部分如下

<bag name="RouteParams" access="property" table="CMS_PluginInstanceRouteParams">
  <key column="ParamId" />
  <element column="Param" type="string" />
</bag>

对于此属性,我们调用时创建的“CMS_PluginInstanceRouteParams”表是正确的:

var schemaExport = new SchemaExport(configuration);
schemaExport.Create(false, true);

但我想知道为什么这个表没有主键。生成的结构是数据库结构

列 ParamId 正确地是类 PluginInstance 的表的外键,并且列中的 Param 正确地是存储的属性 RouteParams 的值。

这张表不需要主键吗?是否可以使用 NHibernate.Mapping.Attributes 设置此属性的主键?

4

1 回答 1

2

该表未映射为实体,因此永远不会自行从数据库中获取,因此 NHibernate 不需要也不会为该表创建主键。

编辑:

如果您想在该表中有一个主键,我建议为此添加一个额外的列。使用 ParamId 作为主键对您不起作用。NHibernate 将使用主键创建表,如果您为RouteParams.

于 2011-04-22T22:40:00.633 回答