DbContext 在实体框架中为空

太阳能

首先,当我尝试访问此列中的数据时出现错误,我不知道原因是什么。

我正在使用 .NET 6 和 EF Core。

我有这门课:

namespace Core.Entities;

public class Usuario 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int IdTecnico { get; set; }
    public string Nombre { get; set; }
    public string Apellido1 { get; set; }
    public string Apellido2 { get; set; }
    public string NIF { get; set; }
    public string EmailPersonal { get; set; }
    public string EmailCorporativo { get; set; }
    public string Direccion { get; set; }
    public string Telefono1 { get; set; }
    public string Telefono2 { get; set; }
    public DateTime FechaRegistro { get; set; }
    public DateTime? FechaAltaEmpresa { get; set; }
    public DateTime? FechaBajaEmpresa { get; set; }
    public string WebContrasena { get; set; }
    public int WebRol { get; set; }
    public int SeguimientNotificacion { get; set; }
    public DateTime? SeguimientoFecha { get; set; }
    public int? SeguimientoIntervalo { get; set; }
    public decimal? EmpresaTarifa { get; set; }
    public int? EmpresaCategoria { get; set; }
    public string ClienteCuenta { get; set; }
    public int? ClienteCategoria { get; set; }
    public int? ClienteNivel { get; set; }
    public string RedmineAPIKey { get; set; }
    public int? RedmineIdProyecto { get; set; }
}

SQL Server 中的表:

在此处输入图像描述

通过这种方式使用类实体dbcontext

namespace Infrastructure.Data;

public class UsuariosContext : DbContext 
{
    public UsuariosContext(DbContextOptions<UsuariosContext> options) : base(options) 
    {
    }

    public DbSet<Core.Entities.Usuario> Usuarios { get; set; }
}

通过依赖注入,我将它注入dbcontext到实现 crud 通用操作的此类中:

namespace Infrastructure.Repositories;

public class UsuarioRepository : IRepository<Core.Entities.Usuario, int> 
{
    UsuariosContext _context;

    public UsuarioRepository(UsuariosContext context) 
    {
        _context = context; 
    }

    public async Task<IReadOnlyList<Usuario>> GetAllAsync() 
    {
        // I get an error because _context.set() operation is null
        return await _context.Set<Usuario>().ToListAsync();
    }
}

所以“表示层”调用这个操作,我得到一个错误:

错误标题E

那是因为_context.set()你看到的操作是空的,但是为什么是空的,我错过了什么吗?

我对其他 db 列的操作完全相同,并且效果很好。

DI配置:

在此处输入图像描述

太阳能

实体属性不允许为空,您应该删除数据库中字段中允许的空值或添加?(空运算符)在模型上

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章