This project is read-only.

NHibernate's "assigned" generator behavior for keys?

Topics: General, Troubleshooting, Writing modules
Mar 3, 2013 at 3:13 AM
Edited Mar 3, 2013 at 3:30 AM
I have several tables that do not need to be content part records (similar to states in this article, but unlike that article, they also need to not have auto-incrementing primary keys.

I have the following model:
public class ItemTypeRecord
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int SortOrder { get; set; }
And the code to add the records into the table:
private readonly IRepository<ItemTypeRecord> _itemTypeRepository;

private readonly IList<ItemTypeRecord> _itemTypes =
    new List<ItemTypeRecord> {
        new ItemTypeRecord {Id = 1, Name = "Audit", SortOrder = 0},
        new ItemTypeRecord {Id = 2, Name = "Response", SortOrder = 1}

public Migrations(IRepository<ItemTypeRecord> itemTypeRepository) {
    _itemTypeRepository = itemTypeRepository;

SchemaBuilder.CreateTable("ItemTypeRecord", table => table
    .Column("Id", DbType.Int16, command => command.PrimaryKey())
    .Column("Name", DbType.AnsiString)
    .Column("SortOrder", DbType.Int16)

foreach (var itemType in _itemTypes)
When Create is called, I receive a SQL error that the 'Id' column is null, even though I have explicitly specified it in the _itemTypes collection. I was hoping there was a command.PrimaryKey().Assigned() call (like Fluent's GeneratedBy.Assigned()), but no such luck.

Is there a way to accomplish what I'm after (short of just writing the SQL by hand)?
Mar 3, 2013 at 6:21 AM
This may be confusing Orchard's conventions. You may have better luck not using automatic convention based mapping and migrations and go against nHibernate directly.