54

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.

1

5 Answers 5

94

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

Sign up to request clarification or add additional context in comments.

6 Comments

What is meant by "the first not-nullable columns"? How many exactly? Certainly not all of them, or else I wouldn't have stumbled over the issue in a view without any non-nullable columns.
That was a sneaky little feature... Thank you for the great fix!
I just ran into this problem and BOOM! Thanks! You'd think EF would have predicted this by now
@jDave1984 In new EF6 i think we will not be able to add view with non nullable column at all.
The link pointed to by "Duplicate Records" is broken.
|
75

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

9 Comments

Thanks a bunch for the AsNoTracking() tip, much easier to add than having to modify our entities for another ROW key :)
This worked for me. (And didn't remove some records where the EDMX update did.)
OMG - ripping my hair out ( figuratively)
Adding AsNoTracking removed duplicate in EF and haven't see any side effects of it, yet.
I hope this thread is still active. Is there a solution for EntityFramework 4.1? It seems like version 4.1 doesn't have the option of AsNoTracking queries. Is there an equivalent solution for 4.1?
|
11

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.

1 Comment

Read the answer of @Juan Carlos Sánchez Robles. ROW_NUMBER s a good way to go but wrap it with a ISNULL function
2

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.

Comments

1

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

1 Comment

was hoping to not have to change edmx, but this regrettably did not work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.