M
M
mletov2017-12-10 10:06:33
ASP.NET
mletov, 2017-12-10 10:06:33

Why is EntityFramework Core returning incorrect values ​​from a view in the database?

Good afternoon.
Connoisseurs of .NET, tell me, please:
I have a view in the database, it returns such a data set
CAT 1 CAT 2 CAT 3 CAT 4 CAT 6 CAT 7 CAT 8 .....
I want to use it in ASP.NET through Code First . As usual, I create a context, model, connection, make a selection

List<v_FullTreeItem> m = db.v_FullTreeItems.OrderBy(x => x.Srt).ToList();

And I get this result
CAT 1 CAT 1 CAT 1 Answer 6-1-2 Answer 6-2-1 CAT 2 CAT 2 CAT 2 CAT 5 CAT 5 CAT 5 CAT 5 ...
The number of records is the same, but the records themselves are different . some records were duplicated, displacing the necessary ones, although all records in the sample are unique.
I must say right away: the view in the database is slightly intricate, it takes data from a procedure that returns a table value, which uses UNION, CTE and another view. With ordinary tables, everything works out, but with this view, some kind of rubbish. And I already did this in an ASP.NET MVC project, there were no problems, so I decided to use ASP.NET Core.
Questions:
Why is this happening? Is this an Entity Framework Core glitch or am I doing something wrong?
- Can it be bypassed?
- Shouldn't it be too late to switch the project back to ASP.NET MVC?
UPD
Tried to go the other way
List<v_FullTreeItem> m = db.v_FullTreeItems.FromSql("SELECT * FROM v_FullTreeItems").ToList();

The result is the same

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mletov, 2017-12-10
@mletov

I solved the problem: I added the ROW_NUMBER() OVER(ORDER BY id) AS PKey field to the view, and indicated in the model that this is the primary key. Everything became normal. It didn't want to work correctly without a primary key.

D
d-stream, 2017-12-10
@d-stream

And what is output if you execute SELECT * FROM v_FullTreeItems for example in management studio

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question