views:

126

answers:

1

I have table CUSTOMER where CUSTOMER_ID is primary key.

Table

CREATE TABLE [CUSTOMER](
    [CUSTOMER_ID] [int] NOT NULL,
    [START_DATE] [varchar(30)] NOT NULL,
CONSTRAINT [PK_CUSTOMER] PRIMARY KEY CLUSTERED ( [CUSTOMER_ID] ASC .....

Mapping

public class CustomerMap : ClassMap<Customer> {
    public CustomerMap()
    {
        WithTable("CUSTOMER");
        Id(x => x.CustomerID, "CUSTOMER_ID");
        Map(x => x.Name, "NAME");
    }
}

I need to handle assigning of the CustomerID property manually. For example I created new Customer with CustomerID = 777 and name "stackoverflow".

When I call method Session.SaveOrUpdate(customer); I want NHibernate to generate me SQL like this:

INSERT INTO [CUSTOMER] ([CUSTOMER_ID] ,[NAME]) VALUES (777,'stackoverflow')

Unfortunately I'm getting errors.

Main issue here is that Nhibernate tries to generate ID for me. But I want to save my entity exactly with 777.

I think there should be way to setup mapping for Id() some another way so this will allow me do what I want.

Please help.

+2  A: 

Depending on how you do your NHibernate mappints (fluent or hbm) you can setup your ID property to manually assign the id. I believe the default is to let the server assign the id.

Derik Whittaker
Thanks.Answer for my question is to manually setup my ID property with adding GeneratedBy.Assigned();Like here: Id(x => x.CustomerID, "CUSTOMER_ID") .GeneratedBy.Assigned();I think that my case is special.
Andriy Buday