Question

I'm using nHibernate to bulk-insert data from XML (fluent)

I build a List from the XML file after reading it into a datatable. I then execute BulkInsert on that list.

akin to this

    // http://stackoverflow.com/questions/982295/saving-1000-records-to-the-database-at-a-time
    public static void BulkInsert(List<dynamic> ls)
    {
        //List<DB.Tables.T_Users> ls = new List<DB.Tables.T_Users>();
        // Read from DataTable


        var sessionFactory = CreateSessionFactory();

        //using (ISession session = sessionFactory.OpenSession())

        // http://davybrion.com/blog/2008/10/bulk-data-operations-with-nhibernates-stateless-sessions/
        using (IStatelessSession session = sessionFactory.OpenStatelessSession())
        {
            using (var tx = session.BeginTransaction())
            {
                session.SetBatchSize(ls.Count);
                foreach (var objThisItem in ls)
                {
                    //session.SaveOrUpdate(objThisItem);
                    session.Insert(objThisItem);
                }
                tx.Commit();
            }
        }
    }

The User-ID (Auto-ID) has just been issued, but everything else is good.

T User here.

CREATE TABLE [dbo].[T_User](
    [USR_ID] [int] IDENTITY(1,1) NOT NULL,
    [USR_Name] [nvarchar](50) NULL,
    [USR_Prename] [nvarchar](50) NULL,
    [USR_User] [nvarchar](50) NULL,
    [USR_Password] [nvarchar](50) NULL,
    [USR_Language] [nvarchar](5) NULL,
    [USR_Hash] [nvarchar](50) NULL,
    [USR_isLDAPSync] [bit] NOT NULL,
    [USR_Domaene] [nvarchar](255) NULL,
    [USR_Hide] [bit] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [USR_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

How can I use nHibernate to insert users while maintaining their IDs? The user-group mapping must be added afterwards, so if IDs change, this will not work.

SqlBulkCopy and SQL Client for.NET

System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity

due to that.

nHibernate has what, though?

And while I'm at it, what purpose does it serve?

System.Data.SqlClient.SqlBulkCopyOptions.KeepNulls
1
0
4/7/2012 5:20:09 PM

Popular Answer

Test accurate mapping using:

mapping => mapping.Id(x => x.Id).GeneratedBy.Assigned()
0
3/19/2012 12:57:29 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow