Cannot convert Implicitly

Asked

Viewed 1,180 times

0

I’m getting the following error:

Unable to convert from "Tclient" to "Easysistema.Models.Applicationclient"

public class ClientManager<TClient> 
{
    private ApplicationDbContext _context;

    public ClientManager(ApplicationDbContext context)
    {
        _context = context;
    }

    public virtual async Task CreateClientAsync(TClient client)
    {
        var result = await _context.Clientes.Add(client);

        if (result.Succeeded)
        {
            _context.SaveChanges();
        }

        return Task.FromResult(result);

    }
}

The call is being made through:

var result = await _clientManager.CreateClientAsync(client);

That is the definition of _clientManager:

    private readonly ClientManager<ApplicationClient> _clientManager;

    public ClientController (
        ClientManager<ApplicationClient> clientManager)
    {
        _userManager = userManager;
    }

Applicationdbcontext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserClaim, ApplicationUserRole,
ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<ApplicationClient> Clientes { get; set; }

And this is my Applicationclient:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace EasySistema.Models
{
public class ApplicationClient
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    [Display(Name = "Tipo Pessoa")]
    public int TipoPessoa { get; set; }
}

Could you please help me, because I can’t see where I’m going wrong, and I can’t fix it.

  • Your class ClientManager is waiting for a class of the type TClient and not ApplicationClient, therefore, in making private readonly ClientManager<ApplicationClient> _clientManager; is causing the error. Unless the class ApplicationClient heiress of TClient, it’s different there.

  • I believe it will be otherwise it is receiving Tclient but expects to receive Easysistema.Models.Applicationclient is not ? so I did private readonly ClientManager<ApplicationClient> _clientManager; in order to pass "Easysistema.Models.Applicationclient".

  • 1

    Or that you’re talking about Generics on TClient were simply T which does not seem to be for the rest of the context

  • Your collection ApplicationDbContext.Clientes is a collection with the type ApplicationClient?

  • Pagotti put my Applicationdbcontext in my question

  • What is the TClient and why you’re using her?

  • My idea was through Tclient pass the parameter EasySistema.Models.ApplicationClient through the construction of the private readonly ClientManager<ApplicationClient> _clientManager; but it’s not working out the way

Show 2 more comments

1 answer

1


I don’t quite understand why Tclient, but come on:

_context.Clientes.Add waits to add a type item Applicationclient. And its class of entity Applicationclient does not inherit from Tclient. So it is not possible to convert one type into another implicitly.

What you can do is explicit conversion:

public virtual async Task<ApplicationClient> CreateClientAsync(TClient client)    
{

    var result =  await _context.Clientes.AddAsync((client as ApplicationClient));     

    if (result.State == EntityState.Added)
    {
        _context.SaveChanges();
    }

    return result.Entity;      
}    

Or else put the definition of the expected type in the class signature:

public class ClientManager<TClient> where TClient : ApplicationClient
{
    private ApplicationDbContext _context;

    public ClientManager(ApplicationDbContext context)
    {
       _context = context;
    }

 public virtual async Task<ApplicationClient> CreateClientAsync(TClient client)    
 {

     var result =  _context.Clientes.Add(client);     

     if (result.State == EntityState.Added)
     {
        _context.SaveChanges();
     }

     return result.Entity;
   }    
}

Or pass the direct type as parameter in the method:

public virtual async Task<ApplicationClient> CreateClientAsync(ApplicationClient client) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.