Ashish
Ashish

Reputation: 437

Can we stop the ID column from auto-incrementing?

I have a table with primary column as an ID. But that column is not an identity column. All the values are provided explicitly.

Below is my mapping class

 public ObjectTypeMap()
    {
        Table("OBJECTTYPE");
        Id(x => x.ObjectTypeId);
        Map(x => x.Title).Not.Nullable();
        Map(x => x.Description);
    }

but when I try to save this object using session.save, I get an error saying

"Cannot insert the value NULL into column 'ObjectTypeId', table 'DiscoveryWEBTest.dbo.ObjectType'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."

But in my object I am providing the value of ObjectTypeId.

The problem is Fluent NHibernate considers ID mapping as auto-increment column, but in the database ObjectTypeId is not an identity column thats why the insert statement do not get any value and throws null error.

Is there a way with which I can tell the the column is an ID column but its value is not auto-incremented?

Thanks in advance

Upvotes: 2

Views: 1847

Answers (1)

David M
David M

Reputation: 72930

Map it like this:

Id(x => x.ObjectTypeId).GeneratedBy.Assigned();

Upvotes: 7

Related Questions