为什么NHibernate创建一个没有主键的表? [英] Why is NHibernate creating a table without primary key?

查看:184
本文介绍了为什么NHibernate创建一个没有主键的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We create the database using the schemaExport class of nHibernate. I now have an class with attributes from which we generated the nhibernate mapping. A part of this class is:

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映射的生成部分如下

The generated part of the nHibernate mapping is the following

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

对于此属性而言,正确的是我们在调用时创建的"CMS_PluginInstanceRouteParams"表:

For this property is correcty the "CMS_PluginInstanceRouteParams" table created when we call:

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

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

But I was wondering why this table does not have an primary key. The generated structure is

ParamId列正确是类PluginInstance的表的外键,而Param列中正确存储的是RouteParams属性的值.

The column ParamId is correctly an foreign key to the table of the class PluginInstance and in the column Param are correctly the values of the property RouteParams stored.

此表上是否不需要主键?是否可以使用NHibernate.Mapping.Attributes在此属性上设置主键?

Is there no need for an primary key on this table? Is it possible to set the primary key on this property using the NHibernate.Mapping.Attributes?

推荐答案

该表未映射为实体,因此,它本身不会从数据库中获取,因此NHibernate不需要并且不会创建主键那个桌子.

The table is not mapped as an entity and as such will never be fetched from the database by itself, therefore NHibernate does not need and will not create a primary key for that table.

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

If you want to have a primary key in that table I suggest adding an extra column for that. Using ParamId as a primary key will not work for you. NHibernate will create the table with a primary key, if you create a class and a corresponding mapping file for RouteParams.

这篇关于为什么NHibernate创建一个没有主键的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆