Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'. #34959

Closed
EduardaMatias opened this issue Oct 23, 2024 · 1 comment

Comments

@EduardaMatias
Copy link

Está me retornando esse erro, mas o Id de container é do tipo inteiro

O erro é nessa função que fica em um repository genérico

        public async Task<T> GetByIdAsync(int id)
        {
            return await _context.Set<T>().FindAsync(id);
        }
    public class ContainerRepository(PortoDbContext context) : Repository<Container>(context)
    {
    }
 public class ContainerMapping : IEntityTypeConfiguration<Container>
 {
     public void Configure(EntityTypeBuilder<Container> builder)
     {
         builder.ToTable("Container");

         builder.Property(e => e.Id).HasColumnName("Id").ValueGeneratedOnAdd();

         builder.Property(e => e.Codigo).HasColumnName("Codigo").IsRequired();
         builder.Property(e => e.Tipo).HasColumnName("Tipo").IsRequired();
         builder.Property(e => e.Status).HasColumnName("Status").HasConversion(typeof(string)).IsRequired();
         builder.Property(e => e.Categoria).HasColumnName("Categoria").HasConversion(typeof(string)).IsRequired();
         builder.Property(e => e.DataCadastro).HasColumnName("DataCadastro").IsRequired();
         builder.Property(e => e.DataAtualizacao).HasColumnName("DataAtualizacao");

         builder.Property(e => e.ClienteId).HasColumnName("ClienteId");
         builder.HasOne(e => e.Cliente).WithMany(pk => pk.Containers).HasForeignKey(e => e.ClienteId);

         builder.HasKey(e => e.Id);
     }
 }
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete([FromRoute] int id)
        {
            return Ok(await this._containerService.Apagar(id));
        }
        public async Task<bool> Apagar(int id)
        {
            Container container = await ConsultarPeloId(id);
            await this._containerRepository.DeleteAsync(container);
            return true;
        }
        public async Task<Container> ConsultarPeloId(int id)
        {
            return await this._containerRepository.GetByIdAsync(id);
        }

Image

Já tentei de tudo, alguém me ajuda por favorrrr

@cincuranet
Copy link
Contributor

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@cincuranet cincuranet closed this as not planned Won't fix, can't repro, duplicate, stale Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants