Entity Framework 6 Select data using ToListAsync doesn't work


I am using Entity framework 6 and my query is as below:

var query = await context.Products.Select(p => new Product() { Id = p.Id, Name = p.Name }).ToListAsync();

it doesn't work and it always break;

but if i remove the "Select(p => new Product() { Id = p.Id, Name = p.Name })

and write like this 

await context.Products.ToListAsync(); and it works.

Whats wrong with that?

 

My Id was Guid

And am doing this with no records in database, I am just checking if my api works.


Asked by:- Aileereal
0
: 6409 At:- 3/18/2018 8:44:23 AM
C# ASP.NET

Is it possible to place the Error details in your question(you have written breaks the code but didn't added the error), without error details, it is always difficult to understand your issue from here. 0
By : Sam - at :- 3/19/2018 8:07:14 AM






2 Answers
profileImage Answered by:- vikas_jk

You must be getting issue The Entity or Complex Type cannot be constructed in a LINQ to Entities Query when executing

var query = await context.Products.Select(p => new Product() { Id = p.Id, Name = p.Name }).ToListAsync();

You should always mention error details when you ask question, so that other users can understand your issue, place all the details of your issue in description when you ask questions

Now back to your question

You cannot (and should not be able to) project onto a mapped entity. You can, however, project onto an annonymous type or onto a DTO

Means you need to create another ViewModel, in your Case Model and ViewModel is same that is "Products"

So, always need to create a New Model(DTO) for fetching data like this

public class ProductDTO
{
    public string Name { get; set; }
    // Other fields if needed here
}

Your C# code should be

var query = await context.Products.Select(p => new ProductDTO() { Name = p.Name ]}).ToListAsync();
1
At:- 3/18/2018 10:28:49 AM
Sorry for this late reply my bad, as i see, i need to create DTO for my product entity to make an strong type (EF6). But why in EF Core i can use entity as DTO. 0
By : Aileereal - at :- 3/20/2018 2:29:56 PM
It doesn't work, because you are mapping same fields again on same class, if you want to use EF generated class, you don't need to create DTO classes at all, just use context.Products.ToListAsync() and in the view .cshtml page you can use directly print data as Model.Name. But it is better to use DTO, as EF generates lots of extra code and classes, which decreases output time, so better to use DTO and map only selected fields which you need in view to print 0
By : vikas_jk - at :- 3/20/2018 6:02:44 PM


profileImage Answered by:- neena

Simply create a data to object Model, to make .ToListAsync work

var getProducts = await context.Products.Select(p => new YourDataModel() { Name = p.Name ]}).ToListAsync();

That's easy, way to resolve error.

0
At:- 11/25/2021 3:49:09 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use