Implicitly can not be converted

0

I'm getting the following error:

  

Can not convert from "TClient" to "EasySystem.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);

This 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; }
}

Please help me, because I can not see where I'm going wrong, and I can not solve it.

    
asked by anonymous 14.09.2018 / 14:54

1 answer

0

I do not quite understand why TClient, but come on:

_context.Clients.Add expects to add an item of type ApplicationClient . And your ApplicationClient entity class does not inherit from TClient . So it is not possible to convert one type to 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 put in the class signature the definition of the expected type:

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 a parameter in the method:

public virtual async Task<ApplicationClient> CreateClientAsync(ApplicationClient client) 
    
17.09.2018 / 22:42