我不知道这个错误意味着什么。我使用的是 Visual Studio for Mac 7.5.0 社区版本。我在 ASP.NET Core 的实体框架中使用延迟加载。
public partial class AdminUser
{
public AdminUser()
{
RoleAssign = new HashSet<RoleAssign>();
}
public Guid UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public byte[] Password { get; set; }
public DateTime CreatedTimeStamp { get; set; }
public DateTime? ModifiedTimeStamp { get; set; }
public DateTime? LogDate { get; set; }
public short? LogNumber { get; set; }
public bool ReloadActiveFlag { get; set; }
public bool IsActive { get; set; }
public string ExtraText { get; set; }
public string ResetPasswordToken { get; set; }
public DateTime? ResetPasswordTokenCreatedTimeStamp { get; set; }
public virtual ICollection<RoleAssign> RoleAssign { get; set; }
}
RoleAssign
实体模型:
public partial class RoleAssign
{
public Guid RoleAssignId { get; set; }
public Guid RoleId { get; set; }
public Guid UserId { get; set; }
public virtual AdminRole Role { get; set; }
public virtual AdminUser User { get; set; }
}
这是实体构建器:
modelBuilder.Entity<RoleAssign>(entity =>
{
entity.Property(e => e.RoleAssignId).ValueGeneratedNever();
entity.HasOne(d => d.Role)
.WithMany(p => p.RoleAssign)
.HasForeignKey(d => d.RoleId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__RoleAssig__RoleI__160F4887");
entity.HasOne(d => d.User)
.WithMany(p => p.RoleAssign)
.HasForeignKey(d => d.UserId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__RoleAssig__UserI__17036CC0");
});
这是用户表的实体构建器:
modelBuilder.Entity<AdminUser>(entity =>
{
entity.HasKey(e => e.UserId);
entity.Property(e => e.UserId).ValueGeneratedNever();
entity.Property(e => e.CreatedTimeStamp)
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.Email)
.IsRequired()
.IsUnicode(false);
entity.Property(e => e.ExtraText).IsUnicode(false);
entity.Property(e => e.FirstName)
.IsRequired()
.IsUnicode(false);
entity.Property(e => e.IsActive)
.IsRequired()
.HasColumnName("isActive")
.HasDefaultValueSql("((1))");
entity.Property(e => e.LastName)
.IsRequired()
.IsUnicode(false);
entity.Property(e => e.LogDate).HasColumnType("datetime");
entity.Property(e => e.ModifiedTimeStamp).HasColumnType("datetime");
entity.Property(e => e.Password).IsRequired();
entity.Property(e => e.ResetPasswordToken).IsUnicode(false);
entity.Property(e => e.ResetPasswordTokenCreatedTimeStamp).HasColumnType("datetime");
entity.Property(e => e.UserName)
.IsRequired()
.IsUnicode(false);
});
伍伦贡大学代码:
public async Task<UserViewModel> AdminAuthentication(UserViewModel userView)
{
var user = await _adminGenericRepository.FindAsync(x => x.IsActive && x.UserName.Equals(userView.UserName) && (AesEncryptAndDecrypt.DecryptStringFromBytes(x.Password, crytograpyKey, crytograpyIV).Equals(userView.Password)));
if (user != null)
{
return new UserViewModel
{
UserId = user.UserId,
isActive = user.IsActive,
UserName = user.UserName,
LastName = user.LastName,
FirstName = user.FirstName,
SelectedRole = mapRoleDbDataToViewModel(user.RoleAssign != null ? user.RoleAssign.FirstOrDefault().Role : null)
};
}
return null;
}
映射器类:
private RoleViewModel mapRoleDbDataToViewModel(AdminRole dbRole)
{
if (dbRole != null)
{
return new RoleViewModel
{
RoleId = dbRole.RoleId,
RoleName = dbRole.RoleName,
RoleType = dbRole.RoleType,
SortOrder = dbRole.SortOrder,
TreeLevel = dbRole.TreeLevel,
Permissions = GetRuleByRoleId(dbRole.RoleId)
};
}
return null;
}
存储库文件:
public virtual async Task<T> FindAsync(Expression<Func<T, bool>> predicate)
{
return await _entities.Set<T>().SingleOrDefaultAsync(predicate);
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}
错误信息截图:
文字记录:
在此上下文中不支持检查 System.Reflection.MethodBase 类型的调试对象中的对象的状态。
据我了解,您在调试的位置发生了这种情况,这是由 Visual Studio 调试器的表达式电梯生成的,因此这可能意味着调试器试图从 System.Reflection.MethodBase 类型的实例中获取数据,但此类对象不是可用,因此产生了该错误,
您可以尝试使用旧版调试引擎,可能会修复它 (工具 -> 选项 -> 调试 -> 常规 -> “使用托管兼容模式”)
不要依赖查找,因为它会在数据库之前进入缓存,特别是在您想要检索相关实体的地方。另外,我相信您对密码的处理是向后的,并且由于解密功能而无法通过 EF/SQL。因此,对用户提供的密码进行加密/散列,并将其与数据库中已加密/散列的数据进行比较。
string encryptedPassword = AesEncryptAndDecrypt.EncryptString(userView.Password, crytograpyKey, crytograpyIV);
var userData = await _adminGenericRepository.FindBy(x => x.IsActive && x.UserName == userView.UserName && x.Password == encryptedPassword)
.Select( new
{
UserId = user.UserId,
IsActive = user.IsActive,
UserName = user.UserName,
LastName = user.LastName,
FirstName = user.FirstName,
// When using FirstOrDefault, you should have an OrderBy to ensure the selection is predictable.
SelectedRole = user.RoleAssign.OrderByDescending(x => x.Date).FirstOrDefault()?.Role
// Cannot call C# methods here since this will go to SQL..
// If you can populate a UserRoleViewModel in-line, then that can be put here to skip the extra mapping below.
}).SingleOrDefaultAsync();
// At this point we will have the user details and it's selected Role ready for mapping.
//This assumes that the mapping of the Role does not rely on any child relationships under the Role.
if (userData != null)
return new UserViewModel
{
UserId = userData.UserId,
IsActive = userData.IsActive,
UserName = userData.UserName,
LastName = userData.LastName,
FirstName = userData.FirstName,
SelectedRole = mapRoleDbDataToViewModel(userData.SelectedRole)
};
else
return null;
在 VS2022 中,只需确保选中选项 抑制模块加载时的 JIT 优化(仅限托管)(在 调试->选项下),(这为我解决了这个问题)。