简体   繁体   English

使用SubSonic SimpleRepository AddMany时获取ID

[英]Get Id back when using SubSonic SimpleRepository AddMany

How can I get the Id back to the object when I'm using the AddMany function with SubSonic SimpleRepository. 当我在SubSonic SimpleRepository中使用AddMany函数时,如何将ID返回到对象。 All my objects still get Id=0 when after using it. 使用它之后,我所有的对象仍然获得Id = 0。

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? 如何在此处对该表中的最新插入ID进行批量选择? Could that ever return the wrong Id? 那会返回错误的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. 问题是我喜欢在另一行(不同表)中将插入的ID用作fk,也许有一种方法可以使用批处理插入来添加两种不同类型的行,并将fk列设置为最后插入的id另一排。 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. 这可能是10k或更多行乘以2。

I use code like this. 我使用这样的代码。 This assumes you've cast your batch of objects to a List , otherwise use a simple foreach on whatever variable you're using: 假设您已将一批对象转换为List ,否则对使用的任何变量使用简单的foreach

using (var ts = new TransactionScope()) {
    thangs.ForEach((t) => {
        t.Id = (int)Repository.Add<Thang>(t);
    });
    ts.Complete();
}

Repository is the instance of your SimpleRepository . Repository是您的SimpleRepository的实例。

It is using the TransactionScope class from System.Transactions (add reference). 它使用System.TransactionsTransactionScope类(添加引用)。 As long as your DB supports it, it should work... 只要您的数据库支持它,它就可以工作...

Ok, so I solved the actual problem: 好的,所以我解决了实际的问题:

IDataProvider provider = ProviderFactory.GetProvider(connectionStringName);

LogEntry entry1 = CreateEntry("1");
LogEntry entry2 = CreateEntry("2");
LogEntry entry3 = CreateEntry("3");
LogEntry entry4 = CreateEntry("4");

List<LogEntry> items = new List<LogEntry>() { entry1, entry2 };

BatchQuery batch = new BatchQuery();
foreach (var item in items)
{
    QueryCommand cmd = item.ToInsertQuery(provider).GetCommand();
    if (item != items.First())
    {
        cmd.CommandSql = cmd.CommandSql.Replace("@ins_LogEntriesfk_LogEntryId", "@@IDENTITY");
    }
    batch.QueueForTransaction(cmd);
}

batch.ExecuteTransaction();

Is this a prefered way on solving the problem. 这是解决问题的首选方法吗? I got another idea, to store all id that's inserted in a table with a GroupId (Guid) and then extract this. 我有另一个想法,就是使用GroupId(Guid)存储插入到表中的所有ID,然后将其提取。 There have to be an easier way to extract all inserted Ids on a connection? 必须有一种更简单的方法来提取连接上所有插入的ID?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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