A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/MapsterMapper/Mapster/issues/258 below:

Nested Interface Projection Not Working · Issue #258 · MapsterMapper/Mapster · GitHub

Hello,

Using .Net Core 3.1 and EF Core 3.1.6.

Given the following entities

public class Product 
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public decimal BasePrice { get; set; }
    public virtual ICollection<ProductTarif> ProductTarifs { get; set; }
}

public class Tarif
{
    public int Id { get; set; }
    public decimal Value { get; set; }
    public virtual ICollection<ProductTarif> ProductTarifs { get; set; }
}

And the following interfaces

public interface IProduct
{
    int Id { get; set; }
    string Name { get; set; }
    decimal BasePrice { get; set; }
    ICollection<ITarif> Tarifs { get; set; }
}

public interface ITarif
{
    int Id { get; set; }
    decimal Value { get; set; }
}

And the following configuration

TypeAdapterConfig<Tarif, ITarif>.NewConfig().PreserveReference(true);

TypeAdapterConfig<Product, IProduct>
    .NewConfig()
    .Map(p => p.Tarifs, p => p.ProductTarifs.Select(pt => pt.Tarif))
    .PreserveReference(true);

I'd expect for the following projection to work, as it does with the object references and Adapt<>,

var projection = ctx.Products.AsNoTracking()
         .Include(p => p.ProductTarifs)
         .ThenInclude(p => p.Tarif)
         .Take(1)
         .ProjectToType<IProduct>()
         .FirstOrDefault();

But instead I'm presented with an exception:

System.ArgumentException: 'Expression of type 'System.Linq.Expressions.Expression`1[System.Func`2[Tryouts.Common.Tarif,GeneratedType_2]]' cannot be used for parameter of type 'System.Linq.Expressions.Expression`1[System.Func`2[Tryouts.Common.Tarif,Tryouts.Common.ITarif]]' of method 'System.Linq.IQueryable`1[Tryouts.Common.ITarif] Select[Tarif,ITarif](System.Linq.IQueryable`1[Tryouts.Common.Tarif], System.Linq.Expressions.Expression`1[System.Func`2[Tryouts.Common.Tarif,Tryouts.Common.ITarif]])' (Parameter 'arg1')'

Am I doing something wrong?

Greatly appreciate your help.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4