Error using $expand with NHibernate and webapi + O

2019-06-08 13:44发布

问题:

When you run the query {{odata-url-prefix}}/ArquivosVenda(2)?$expand=Vendas an error is generated:

{
    "odata.error": {
        "code": "",
        "message": {
            "lang": "en-US",
            "value": "An error has occurred."
        },
        "innererror": {
            "message": "Argument types do not match",
            "type": "System.ArgumentException",
            "stacktrace": "   at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
        }
    }
}

I noticed that the error occurs when using the $expand; Queries as {{odata-url-prefix}}/ArquivosVenda(2) and {{odata-url-prefix}}/ArquivosVenda(2)/Vendas normally operate, only queries with $expand is that this problem occurs.

Code

Domain

public class ArquivoVenda: IAuditavel, IEntidade
{
    public virtual int Id { get; set; }
    public virtual string FileName { get; set; }
    public virtual ICollection<Venda> Vendas { get; set; }
}


public class Venda : IAuditavel, IEntidade
{
    public virtual int Id { get; set; }

    public virtual DateTime Data { get; set; }
    public virtual string GSM { get; set; }
    public virtual string Plano { get; set; }
}

Controller

[Authorize]
public abstract class BaseEntidadeController<TEntidade> : ODataController
    where TEntidade : class, IEntidade
{
    protected IRepositorio<TEntidade> Repositorio { get; private set; }

    public BaseEntidadeController(IRepositorio<TEntidade> repositorio)
    {
        Repositorio = repositorio;
    }

    [HttpGet, Queryable(AllowedQueryOptions = AllowedQueryOptions.All, PageSize = 25, MaxExpansionDepth = 3)]
    public IQueryable<TEntidade> Get()
    {
        return Repositorio.All();
    }

    [Queryable(MaxExpansionDepth = 3)]
    public virtual SingleResult<TEntidade> Get([FromODataUri]int key)
    {
        return SingleResult.Create<TEntidade>(Repositorio.Query(c => c.Id == key));
    }

    [HttpGet]
    protected virtual TEntidade GetEntityByKey(int key)
    {
        return Repositorio.All().FirstOrDefault(p => p.Id == key);
    }
}

public class ArquivosVendaController  : BaseEntidadeController<ArquivoVenda>
{
    public ArquivosVendaController(IArquivosVendaRepositorio repositorio)
        : base(repositorio)
    { }

    public IQueryable<Venda> GetVendas([FromODataUri] int key)
    {
        return Repositorio.Get(key).Vendas.AsQueryable();
    }
}

WebApi configuration

...
var arquivoVenda = modelBuilder.EntitySet<ArquivoVenda>("ArquivosVenda");
arquivoVenda.EntityType.HasKey(p => p.Id);

var venda = modelBuilder.EntitySet<Venda>("Vendas");
venda.EntityType.HasKey(p => p.Id);
...