简体   繁体   中英

Returning last inserted id in C# using Repository

In a .NET Core / C# application, using the Boilerplate framework I use repositories for data access:

await _licenseRepository.InsertAsync(license);

But in a method I need to do two inserts in two different tables, and then do the relationship:

public async Task Insert(License license, LicenseRenewal licenseRenewal)
{
    var l = await _licenseRepository.InsertAsync(license);
    var lr = await _licenseRenewalRepository.InsertAsync(licenseRenewal);
    await AssignRenewal(l.Id, lr.Id);
}

The problem is that when I do the Insert I do not get the IDs of the new data and so it fails to do the AssignRenewal .

How do I get the ID of the inserted data?

aspnetboilerplate.com/Pages/Documents/Repositories#insert states:

"[...] The InsertAndGetId method returns the Id of a newly inserted entity.[...]"

So you should try

Task<TPrimaryKey> InsertAndGetIdAsync(TEntity 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