Despite I use Take() method, EF Core return all data

.net-core c# entity-framework-core

Question

I have a view in my DB. I have created model of view and call it with EF Core. There are 16 rows in view. When I call it with EF Core and use Take(10) it returns 10 rows but I look DBSet all rows shows there. For detail here is my code

[Table("my_view", Schema ="dbo")]
public class MyView
{

    [Column("id")]
    [Key]
    public int Id { get; set; }

    [Column("name")]
    public string Name { get; set; }

}

in my action:

public JsonResult Index()
{
    using (var context = new CCContext())
    {
        var listOfData = context.MyView
                            .AsQueryable().Take(10).ToList();
        return Json(listOfData);
    }
}

PS. For some problem I couldn't upload screen of result.(https://m.imgur.com/gallery/xmP3q32)

https://imgur.com/a/xmP3q32

1
-1
3/4/2019 10:36:09 AM

Accepted Answer

If you mean context.MyView contains all rows, then you are right. Because that is a representation of the table in the DB. However, in your example listOfData should contain only 10 rows. If so, then this is as it should be expected.

If the above does not answer your question, please give some more insight and also, what DB provider are you using with EF?

For clarification: The data is not there untill you ask a debugger (or code) to fetch it for you. The only reason you see it is because you ask the debugger to show its contents, and therefor it fetches its contents. It wont if you wont ask it to.

7
2/19/2019 7:49:18 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow