开发者

I want generate INSERT which include my Primary Key (not generated) by NHibernate. How to map Id column for this?

开发者 https://www.devze.com 2022-12-19 04:59 出处:网络
I have table CUSTOMER where CUSTOMER_ID is primary key. Table CREATE TABLE [CUSTOMER]( [CUSTOMER_ID] [int] NOT NULL,

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 manu开发者_如何学Cally. 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消