How can I get the Id back to the object when I'm using the AddMany function with SubSonic SimpleRepository. All my objects still get Id=0 when after using it.
SimpleRepository repository = new SimpleRepository(ConnectionStringName);
repository.AddMany<T>(insertList);
When looking at the source I can see that:
public void AddMany<T>(IEnumerable<T> items) where T : class, new()
{
if (_options.Contains(SimpleRepositoryOptions.RunMigrations))
{
Migrate<T>();
}
BatchQuery batch = new BatchQuery(_provider);
foreach(var item in items)
{
batch.QueueForTransaction(item.ToInsertQuery(_provider));
}
batch.ExecuteTransaction();
}
How about making a batch select for latest inserted Id in that table here? Could that ever return the wrong Id? I will write down some code and come back :)
The actual problem
The problem is that I like to use the inserted Id's in another row (different table) as a fk, maybe there is a way to use batch insert to add two different kinds of rows and set the fk-column to the last inserted id of the other row. A bit complicated there, but I think you get the point:
Insert User
Insert UserAccount -> Set UserAccount.fk_UserId to latest id inserted in User
Insert User
Insert UserAccount -> Set UserAccount.fk_UserId to latest id inserted in User
And so on as a batch.. is that possible? This could be as much as 10k or more rows times 2.