[英]SaveChangesAsync in EF Core IdentityDbContext
I'm trying to use Entity Framework Core Identity in an ASP.NET Core application 我正在尝试在ASP.NET Core应用程序中使用Entity Framework Core Identity
I have created Database Context and its interface as follows: 我创建了数据库上下文及其接口,如下所示:
public class AppDbContext : IdentityDbContext<AppUser>, IAppDbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options) : base(options)
{ }
public DbSet<AppUser> AppUser { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
public interface IAppDbContext
{
DbSet<AppUser> AppUser { get; set; }
int SaveChanges();
Task<int> SaveChangesAsync();
}
Here the issue is, it shows an error in AppDbContext
, stating 问题出在这里,它显示
AppDbContext
中的错误,指出
'AppDbContext' does not implement interface member 'IAppDbContext.SaveChangesAsync()'
'AppDbContext'未实现接口成员'IAppDbContext.SaveChangesAsync()'
If the AppDbContext is inherited from the DbContext
insted of IdentityDbContext
error would not appear, but to use Identity it should be inherited from IdentityDbContext
. 如果AppDbContext从继承
DbContext
insted的的IdentityDbContext
错误不会出现,但使用的身份应该被继承IdentityDbContext
。
How can I get around this? 我该如何解决?
It's weird, because this error should shown in both cases. 很奇怪,因为在两种情况下都应显示此错误。 Anyway,
DbContext
doesn't have method: 无论如何,
DbContext
没有方法:
Task<int> SaveChangesAsync()
It has method: 它具有方法:
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
To get around this case, you should wrap DbContext.SaveChangesAsync
method: 要避免这种情况,您应该包装
DbContext.SaveChangesAsync
方法:
public class AppDbContext : IdentityDbContext<AppUser>, IAppDbContext
{
...
public Task<int> SaveChangesAsync() => base.SaveChangesAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.