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

UseAsDataSource OData expand trows Argument types do not match exception #60

Closed
mgdmiller opened this issue Mar 7, 2020 · 7 comments
Closed

Comments

@mgdmiller
Copy link

Hi!

I have simple project that uses odata. I decided to use an automapper because ist very simple. But i have problem with function UseAsDataSource(). When i use ProjectTo() all work fine but EF cannot evaluate filter expressions in db and go it locally. But when i use UseAsDataSource() its throw Argument types do not match exception when specifying $expand. I would like to use the functionality UseAsDataSource() due to performance.

Source/destination types

// DB Objects below

public abstract class BaseDomainObject
{
	[Key]
	public long Id { get; set; }
}

public class Order : BaseDomainObject
{
	public virtual ICollection<OrderPosition> Positions { get;  } = new List<OrderPosition>();
}

public class OrderPosition : BaseDomainObject
{
	public virtual Order Order { get; set; }

	public int Amount { get; set; }
}

// DTO Objects below

public abstract class ApiModel
{
	public long Id { get; set; }
}

public class OrderModel : ApiModel
{
	public IEnumerable<OrderPositionModel> Positions { get; set; }
}

public class OrderPositionModel : ApiModel
{
	public int Amount { get; set; }
}

Mapping configuration

// Profile code

public class OrderProfile : Profile
{
	public OrderProfile()
	{
		CreateMap<BaseDomainObject, ApiModel>();
		CreateMap<Order, OrderModel>().IncludeBase<BaseDomainObject, ApiModel>();
		CreateMap<OrderPosition, OrderPositionModel>().IncludeBase<BaseDomainObject, ApiModel>();
	}
}
	
// Startup code
	
public void ConfigureServices(IServiceCollection services)
{
	services.AddDbContext<OrdersContext>(o =>
	{
		o.UseInMemoryDatabase("test");
		o.UseLazyLoadingProxies();
	});

	services.AddOData();
	services.AddAutoMapper( GetType());
	
	services.AddMvc(options => { options.EnableEndpointRouting = false; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

AutoMapper Version: 9.0.0

EF Version: 2.2.6

Expected behavior

Expected list expansion when specifying $expand=positions

Actual behavior

Throws Argument types do not match

Steps to reproduce

I create demonstration project

Simply call http://localhost:5000/api/orders?$expand=positions

Best regards. Vitaly

@lbargaoanu lbargaoanu transferred this issue from AutoMapper/AutoMapper Mar 7, 2020
@lbargaoanu
Copy link
Member

A PR is welcome.

@BlaiseD
Copy link
Member

BlaiseD commented Mar 7, 2020

Have you considered the OData Extensions library?

Or creating your own mapping extensions like the ReadMe examples?

@mgdmiller
Copy link
Author

Have you considered the OData Extensions library?

Or creating your own mapping extensions like the ReadMe examples?

OData extensions work perfectly! I try to use GetAsync method but it loading all set ignoring PageSize controller option. But GetQueryAsync works as it should.

[EnableQuery]
public async Task<IQueryable<TModel>> Get(ODataQueryOptions<TModel> options)
{
	return await _context.Set<TEntity>().GetQueryAsync(_mapper, options, HandleNullPropagationOption.False);
}

Thank you very much for your help!

@BlaiseD
Copy link
Member

BlaiseD commented Mar 7, 2020

Sounds great. Just FYI EnableQuery conflicts with the extension - you could be performing some operations twice. The EnableQuery attribute should be removed. Use $top for page size e.g. http://localhost:1234/controller?$top=5.

@mgdmiller
Copy link
Author

Thanks for the clarification! I checked the behavior of EnableQuery, in my conditions it works correctly. The TOP is not a hard limit, which can lead to fetching the entire table. But I will take this into account in the future!

@BlaiseD
Copy link
Member

BlaiseD commented Mar 8, 2020

Just so other users get the correct information, do not use the EnableQuery attribute with AutoMapper.Extensions.OData. See the ReadMe documentation and issue #12 at AutoMapper.Extensions.OData. If we're missing a test case, please open an issue at AutoMapper.Extensions.OData.

@mgdmiller
Copy link
Author

It is interesting. I publish project to test environment and collect information for explain reply. It will take some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants