Why is NHibernate creating a table without primary key?

◇◆丶佛笑我妖孽 提交于 2019-12-10 11:41:45

问题


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; }
    }
    ...
}

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>

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

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.

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?


回答1:


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.

Edit:

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.



来源:https://stackoverflow.com/questions/5755079/why-is-nhibernate-creating-a-table-without-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!