在此上下文中不支持检查System.Reflection.MethodBase类型的调试对象中的对象的状态

问题描述 投票:11回答:2

我不知道这个错误意味着什么。我,正在使用visual studio for mac 7.5.0社区版。从我开始,我在asp.net核心的实体框架中使用延迟加载。

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; }
}

角色分配实体模型

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);
            });

UOW Code

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;
        }

这是错误的屏幕截图。

error image

不知道此错误的含义 - 在此上下文中不支持检查System.Reflection.MethodBase类型的调试对象中的对象的状态。

c# asp.net entity-framework asp.net-core visual-studio-mac
2个回答
1
投票

根据我的理解你在哪里进行调试,这是从Visual Studio调试器的表达式电梯产生的,这可能意味着调试器试图从System.Reflection.MethodBase类型的实例获取数据,但是这样的对象不可用所以它产生了这个错误,

您可以尝试使用旧版调试引擎,可能会修复它(工具 - >选项 - >调试 - >常规 - >“使用托管兼容模式”)


0
投票

不要依赖于Find,因为它会在DB之前进入缓存,特别是在您想要检索相关实体的位置。此外,我认为您对密码的处理是倒退的,并且由于解密功能而无法通过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;
© www.soinside.com 2019 - 2024. All rights reserved.