我正在尝试使用SaveChangesAsync方法将数据保存到数据库中, 首先,用户正在登录(用户的isOnline bool属性为true并将更改保存到数据库) 但是当我注销时,我使用完全相同的方法(将isOnline设置为false)然后在SaveChangesAsync-SQLiteException弹出时, 我查看了异常信息,并说数据库已被锁定。
public readonly ProjectDbContext _projectDbContext;
public UserRepository(ProjectDbContext projectDbContext)
{
_projectDbContext = projectDbContext;
}
public async Task<User> Logout(User user)
{
var userList = _projectDbContext.Users.ToList();
foreach (var userX in userList)
{
if (userX.Id == user.Id)
{
userX.IsOnline = false;
try
{
await _projectDbContext.SaveChangesAsync();
}
catch (SqliteException e)
{
var str = e.Data;
return userX;
}
return userX;
}
}
return null;
}
public async Task<User> GetUserAsync(User user)
{
var userList = _projectDbContext.Users.ToList();
foreach (var userX in userList)
{
if (userX.UserName == user.UserName && userX.Password == await HashMD5(user.Password))
{
userX.IsOnline = true;
await _projectDbContext.SaveChangesAsync();
return userX;
}
}
return null;
}
异常消息:
{Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 5: 'database is locked'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.Data.Sqlite.SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText)
at Microsoft.Data.Sqlite.SqliteTransaction..ctor(SqliteConnection connection, IsolationLevel isolationLevel)
at Microsoft.Data.Sqlite.SqliteConnection.BeginTransaction(IsolationLevel isolationLevel)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionWithNoPreconditions(IsolationLevel isolationLevel)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple`2 parameters, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList`1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at ServerSideAngularProject.DAL.UserRepository.Logout(User user) in C:\Users\osher\source\repos\ServerSideAngularProject\ServerSideAngularProject\DAL\UserRepository.cs:line 33}