ASP.NET Entity Framework查询时IsActivated数据库为true却返回false
问题描述
我正在使用ASP.NET和Entity Framework开发API,尝试从数据库中获取一条记录。该记录的IsActivated列值为1(对应布尔值true),但输出到控制台时,这个布尔值却显示为False。除IsActivated字段外,其他返回值均正确,且Entity Framework能够正常更新该列。
User.cs
public class User : IEntity { public Guid Id { get; set; } public string Email { get; set; } public string Password { get; set; } public bool IsActivated { get; set; } }
Repository.cs
public class Repository<T> : IRepository<T> where T: class, IEntity { private EntityContext _context; public Repository(EntityContext context) { _context = context; } public async Task<List<T>> Get( Expression<Func<T, bool>>? filter = null, params Expression<Func<T, object>>[] includes) { IQueryable<T> dbSet = _context.Set<T>(); if (filter != null) dbSet = dbSet.Where(filter); foreach (var expression in includes) dbSet = dbSet.Include(expression); return await dbSet .ToListAsync(); } }
AuthService.cs
public class AuthService { private readonly UserRepository _userRepository; private readonly PasswordHasher<User> _passwordHasher; private readonly TokenService _tokenService; public AuthService(UserRepository userRepository, TokenService tokenService) { _userRepository = userRepository; _passwordHasher = new PasswordHasher<User>(); _tokenService = tokenService; } public async Task<User> GetUserByEmail(string email) { List<User> users = await _userRepository.Get(user => user.Email == email); if (users.Count <= 0) throw new EntityNotFoundException(); return users.First(); } }
已尝试的解决方法
- 将Entity Framework从6.0.13升级到7.0.0,程序完全崩溃(推测是因为使用.NET 6导致)
- 将Entity Framework从6.0.13降级到6.0.10,无任何效果
- 创建新方法专门测试获取数据库数据,问题依旧存在
内容的提问来源于stack exchange,提问作者jwaw
相关产品推荐
相关产品推荐

