ASP.NET Entity Framework在数据库中的值为true时返回false。

huangapple go评论44阅读模式
英文:

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确实正确更新了列...

我已经尝试过的事情

  1. 将Entity Framework从6.0.13升级到7.0.0,但这破坏了一切(可能因为我在dotnet 6上)。
  2. 将Entity Framework从6.0.13降级到6.0.10,但没有任何效果。
  3. 通过新创建的方法从数据库中检索数据,只是为了测试,但没有效果。
英文:

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&lt;T&gt; : IRepository&lt;T&gt; where T: class, IEntity
{
    private EntityContext _context;

    public Repository(EntityContext context)
    {
        _context = context;
    }

    public async Task&lt;List&lt;T&gt;&gt; Get(
        Expression&lt;Func&lt;T, bool&gt;&gt;? filter = null, 
        params Expression&lt;Func&lt;T, object&gt;&gt;[] includes)
    {
        IQueryable&lt;T&gt; dbSet = _context.Set&lt;T&gt;();

        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&lt;User&gt; _passwordHasher;

    private readonly TokenService _tokenService;

    public AuthService(UserRepository userRepository, TokenService tokenService)
    {
        _userRepository = userRepository;

        _passwordHasher = new PasswordHasher&lt;User&gt;();

        _tokenService = tokenService;
    }

    public async Task&lt;User&gt; GetUserByEmail(string email)
    {
        List&lt;User&gt; users = await _userRepository.Get(user =&gt; user.Email == email);

        if (users.Count &lt;= 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

  1. Updating EntityFramework from 6.0.13 to 7.0.0, it broke everything (probably because I'm on dotnet 6).
  2. Downgrading EntityFramework from 6.0.13 to 6.0.10, didn't do anything.
  3. 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...

huangapple
  • 本文由 发表于 2023年2月10日 05:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定