Nhibernate Identity Column - Change mapping?

Topics: Core, Customizing Orchard
Feb 21, 2013 at 2:19 PM
Hi all -

I created a module called "Tools" and I'm at the point where an admin has entered the data for the "Main Tool" Content Type....but I'm getting this error on saving/publishing:
Cannot insert explicit value for identity column in table 'Tools_ToolPartRecord' when IDENTITY_INSERT is set to OFF.
Here's the migration.cs
             SchemaBuilder.CreateTable("ToolPartRecord", tbl => tbl
                                   .Column<int>("Id", col => col.PrimaryKey().Identity())
                                   .Column<string>("SubHeader", col => col.WithLength(300))
I think I need to change the NHibernate mapping to set the generator = native. Where can I do this in orchard? I've found where mappings for FK are set in ContentPartAlteration.cs, but I'm not sure if this is the right spot.

Any help/suggestions would be GREATLY appreciated.
Feb 21, 2013 at 7:28 PM
You should not have to do that. Could you share the code that instantiates and inserts the entity?
Feb 21, 2013 at 10:02 PM
Thanks for the response!

So as I was copying in my code, I noticed what I think is the issue

public class ToolPartRecord : ContentPartRecord
   public virtual string Header { get; set; }
   public virtual string SubHeader { get; set; }
   public virtual string Type { get; set; }
I define my model to be a ContentPartRecord, so I'm creating two references to the "Id" column. I thought if I left out the CPR declaration in the migration, I wouldn't use the already existing Id. So that would explain the SQL error.

What I would like to know then: I there is a way to configure the CPR to have "Id" as an identity? Or would I have to create my own implementation and repository using IDependency to achieve this?
Feb 25, 2013 at 10:59 PM
Edited Feb 25, 2013 at 11:00 PM
I don't think you should do that, because the "Id" column of a content part record is what binds itself with a content item: they share the ID value.
If the "Id" column of your content part were an Identity, while Orchard assigns it the ID value of its content item, you will get the error you mentioned in your first post.

Why do you want to turn "Id" into an Identity anyway?