0

I'm working on an MVC/NHibernate/SQL application where I need to insert multiple rows (generated dynamically) into the same table while giving them the same group id. Let's say I have the table with

fields(id[Identity], date, amount, group_Id)

How do I figure out a group_id to identify group entries?

As I understand, querying the max group id wouldn't be the solution as many users might access the application at the same time thus generate the same group id. What would be the best way to do it?

4

1 回答 1

0
IList<Entity> group = ...;

var firstId = (int)session.Save(group[0]);
group[0].GroupId = firstId;

foreach(var e in group.Skip(1))
{
    e.GroupId = firstId;
    session.Save(e);
}
session.Flush();

这确实适用于几乎每个 Id 生成和数据库。这取决于第一个条目的唯一 ID

于 2013-03-13T13:45:01.790 回答