2017-08-11 75 views
0

我使用默认模型类和SQLite作为数据库的asp.net mvc核心web应用程序。 我对下面的代码删除用户:在asp.net mvc核心中删除用户的奇怪例外

var user = await _dbContext.Users.SingleOrDefaultAsync(s => s.Id == id); 
try 
{ 
    await _userManager.DeleteAsync(user); 
} 
catch (Exception) 
{ 
} 

删除大多数用户都不错,但对于某些用户我得到了不同的异常,如:System.ObjectDisposedExceptionSystem.InvalidOperationException: 'BeginTransaction can only be called when the connection is open.'在这种情况下,用户不会被删除。为什么相同的代码对于不同的用户来说可能很奇怪?

回答

0

问题是,我有类也存储在DB:

public class Coordinate 
{ 
    public int ID { get; set; } 
    public double Lat { get; set; } 
    public double Lon { get; set; } 

    [JsonIgnore] 
    public ApplicationUser User { get; set; } 
} 

所以,如果我想删除用户,其中有坐标 - 我得到一个错误。我必须先删除用户的坐标:

  var coordinates = _dbContext.Coordinates.Where(x => x.User == user).ToList(); 
      _dbContext.Coordinates.RemoveRange(coordinates);