1

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 database structure

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?

Wessel Kranenborg
  • 1,400
  • 15
  • 38
  • If you define the ParamId column as primary key you will lose the 1:n relationship, won't you? Why do you want a primary key in that table? – Florian Lim Apr 22 '11 at 22:40
  • where is your entity defined? –  Apr 22 '11 at 23:28
  • I was wondering why this table has no primary key because we use de vs2010 datacomparer to copy some initial data from the staging database to live database. But this table does not show up in the comparer because of the primary key. This question goes about that: http://stackoverflow.com/questions/5700864/visual-studio-2010-datacompare-table-comparison – Wessel Kranenborg Apr 23 '11 at 00:39

1 Answers1

2

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.

Florian Lim
  • 5,332
  • 2
  • 27
  • 28