简体   繁体   中英

last_insert_id ? C#, SQL Server and NHibernate

I need to know how can I know what is the last inserted id in a SQL Server database for a given table?

I'm using int identity not guid and if it's matters NHibernate as my ORM.

Thanks

You could try something like this:

var sql = "SELECT IDENT_CURRENT('TableName')";
var query = session.CreateSQLQuery(sql);
var result = query.UniqueResult();

This will return the last identity value generated for a given table.

根据您的ID的生成方式,您应该可以直接从ISession.Save()返回它:

int insertedID = (int)session.Save(entity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM