Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unbelievable duplicate in an Entity Framework Query

My SQL query against a particular view returns me 3 different rows.

 select * from vwSummary
 where vidate >= '10-15-2010' and vidate <= '10-15-2010'
 and idno = '0330'
 order by viDate

But if i run the same query through my entity framework, I get 3 rows but all the 3 rows are same, equivalent to the third row.

        firstVisibleDate = new DateTime(2010, 10, 15);

        lastVisibleDate = new DateTime(2010, 10, 15);

var p1 = (from v in db.vwSummary
                     where v.viDate >= firstVisibleDate && v.viDate <= lastVisibleDate
                     && v.IDNo == "0330"
                          select v).ToList();

Can someone please help me to resolve this issue.

EDIT: I changed my query like this and it works. But still I want to go back to the one shown above as I have to iterate again for more processing.

List<objectName> p1 = (from v in db.vwSummary
                     where v.viDate >= firstVisibleDate && v.viDate <= lastVisibleDate
                     && v.IDNo == "0330"
                          select new <ObjectName>
{
a = v.a
b = v.b
}
).ToList<ObjectName>();
like image 924
franklins Avatar asked Feb 14 '11 15:02

franklins


People also ask

How can we avoid duplicate records in a query?

The SQL DISTINCT keyword, which we have already discussed is used in conjunction with the SELECT statement to eliminate all the duplicate records and by fetching only the unique records.

How do you avoid duplicates 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.

Which clause is used to remove duplicate values from a query?

Answer: The DISTINCT keyword is used to remove the duplicate rows in a table.


2 Answers

I had a similar issue and I solved it by changing the merge option of the ObjectSet. Example:

    using (TargetDBDataContext db = new TargetDBDataContext())
    {
        db.SomeView.MergeOption = System.Data.Objects.MergeOption.NoTracking;
        return db. SomeView.ToList();
    }

It looks like entity framework(EF) doesn’t handle correctly views that have duplicated primary keys or no primary keys at all. So when there are two rows that EF is considering equal, EF will load first row as it should but will not load the second row because it will consider it’s already loaded.

like image 92
cibis Avatar answered Oct 13 '22 20:10

cibis


Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking(). This optimisation allows you to tell Entity Framework not to track the results of a query. This means that Entity Framework performs no additional processing or storage of the entities which are returned by the query. However it also means that you cant update these entities without reattaching them to the tracking graph.

You can set AsNoTracking option directly on your view to resolve this issue.

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

like image 22
Rahul Garg Avatar answered Oct 13 '22 20:10

Rahul Garg