Entity Framework. View return duplicate records

Entity FrameworkDuplicates

Entity Framework Problem Overview


I use Entity Framework that contains view. And I have query:

var data = this.context.vwRevenues
    .Where(x => x.revenue >= 0);
    .OrderByDescending(x => x.year)
    .ThenByDescending(x => x.month)
    .Take(10)
    .ToList();

This query returns set of entities, but 1st entity equals 5th.

data[0] == data[4] // true

I take sql script for this query from sql tracer and run it into SQL Management Studio, it returns different records.

Entity Framework Solutions


Solution 1 - Entity Framework

As per @Giovane Answer's

We had the same problem in our system with Entity Framework dealing with views. Try using ROW_NUMBER () OVER () SQL to create a column with unique values​​, but did not work.

I did the same thing but to make it work i need to open the EDMX model and then select a this column as an Entity Key.

enter image description here

Then it will work

There is a very good article on this

Duplicate Records

The articles most important line is:

When including a view in your Entity Model, the model seems to simply use the first not-nullable columns as primary key (as all columns used in the primary key should be non-nullable).

Solution 2 - Entity Framework

You only need to do: context.viewname.AsNoTracking().Where(x => x.ColumnName != null);

Solution 3 - Entity Framework

We had the same problem in our system with Entity Framework dealing with views. Try using ROW_NUMBER () OVER () SQL to create a column with unique values​​, but did not work.

We need to insert a field more, an FK for another table in the view so that it could add as an additional training for mebro EntityKeyMembers Elimite and so the problem of repetition.

Thus, if the problem persists in this kind of situation, the solution is to insert a FK column for it to be MEMBERS of the fields that form the EntityKey of the table.

Solution 4 - Entity Framework

In the view try converting the first record to a not null value, like this:

isnull(ROW_NUMBER() OVER (ORDER BY "Column"), 0) AS Row

It indicates to the Entity Framework that can be the primary key automatically.

Solution 5 - Entity Framework

If you don't want to update edmx and set any key to column &&

if you don't want to update view record (only for get record) then use below code its working for me.

context.viewname.MergeOption = System.Data.Objects.MergeOption.NoTracking;

context.viewname.Where(x => x.columnname != null);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEugene GluhotorenkoView Question on Stackoverflow
Solution 1 - Entity FrameworkMoonsView Answer on Stackoverflow
Solution 2 - Entity FrameworkRahul GargView Answer on Stackoverflow
Solution 3 - Entity FrameworkGiovaneView Answer on Stackoverflow
Solution 4 - Entity FrameworkJuan Carlos Sánchez RoblesView Answer on Stackoverflow
Solution 5 - Entity FrameworkRikin PatelView Answer on Stackoverflow