Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework. View return duplicate records

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.

like image 872
Eugene Gluhotorenko Avatar asked Oct 20 '10 12:10

Eugene Gluhotorenko


People also ask

How to avoid duplicate records in Entity Framework?

Duplicate rows in Entity Framework SQL View Entity Framework auto set fields as entity key for those not null column and return the row that match those entity key that causes the problem. You can set AsNoTracking option directly on your view to resolve this issue.

How to prevent duplicate entries in database in mvc?

best way is to query for a record with the same FirstName (using FirstOrDefault() to get only the first one or better Count()) and do not even try to insert the record.

What difference does AsNoTracking () make?

AsNoTracking(IQueryable)Returns a new query where the entities returned will not be cached in the DbContext or ObjectContext. This method works by calling the AsNoTracking method of the underlying query object.


4 Answers

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).

like image 56
Moons Avatar answered Sep 23 '22 21:09

Moons


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

like image 43
Rahul Garg Avatar answered Sep 22 '22 21:09

Rahul Garg


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.

like image 12
Giovane Avatar answered Sep 23 '22 21:09

Giovane


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.

like image 2
Juan Carlos Sánchez Robles Avatar answered Sep 20 '22 21:09

Juan Carlos Sánchez Robles