简体   繁体   中英

How to Override SaveChangesAsync in Net Core?

How do I override SaveChangesAsync in Net Core Entity Framework? Receiving the error below,

public override async Task<int> SaveChangesAsync()
{
    ApplyAuditInformation();
    return await base.SaveChangesAsync();
}

Error: 'TestDBContext.SaveChangesAsync()': no suitable method found to override

The signature of SaveChangesAsync requires a CancellationToken .

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
    ApplyAuditInformation();
    return base.SaveChangesAsync(cancellationToken);
}

You'd only need to declare the method as async if you need to await something.

For example, if your ApplyAuditInformation needs to be " await ed":

public async override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
    await ApplyAuditInformationAsync();
    return base.SaveChangesAsync(cancellationToken);
}

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