英文:
ASP.NET Entity Framework boolean returns false while the value is true in the database
问题
我目前正在使用ASP.NET和Entity Framework开发API。我正在尝试从数据库中获取一条记录。记录包含一个名为IsActivated
的列,该列设置为1
(true)。但是,每当我将它写入控制台时,布尔值都会被设置为False
。
我的代码如下:
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();
}
}
返回值是正确的,除了IsActivated
。Entity Framework确实正确更新了列...
我已经尝试过的事情
- 将Entity Framework从6.0.13升级到7.0.0,但这破坏了一切(可能因为我在dotnet 6上)。
- 将Entity Framework从6.0.13降级到6.0.10,但没有任何效果。
- 通过新创建的方法从数据库中检索数据,只是为了测试,但没有效果。
英文:
I'm currently developing an API using ASP.NET and Entity Framework. I'm trying to get one record from the database. The record contains one column named IsActivated
which is set as 1
(true). However, the boolean is set to False
whenever I write it to my console.
My code is as follows:
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();
}
}
The return value is correct except for the IsActivated
. EntityFramework does update the column correctly...
What I already tried
- Updating EntityFramework from 6.0.13 to 7.0.0, it broke everything (probably because I'm on dotnet 6).
- Downgrading EntityFramework from 6.0.13 to 6.0.10, didn't do anything.
- Retrieving the data from the database through a newly created method just to test, didn't work.
答案1
得分: 0
我在User构造函数中将变量分配给自身,而不是分配给给定的值...
英文:
Turned out I assigned a variable to it self in the User constructor rather than assigning the given value...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论