首先,當我嘗試訪問此列中的資料時出現錯誤,我不知道原因是什么。
我正在使用 .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();
}
}
所以“表示層”呼叫這個操作,我得到一個錯誤:

那是因為_context.set()你看到的操作是空的,但是為什么是空的,我錯過了什么嗎?
我對其他 db 列的操作完全相同,并且效果很好。
DI配置:

uj5u.com熱心網友回復:
物體屬性不允許為空,您應該洗掉資料庫中欄位中允許的空值或添加?(空運算子)在模型上
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451610.html
上一篇:在這種情況下,如何在EFCore中設定onDelete級聯?
下一篇:物體框架6未來正在生成重復查詢
