Incredibly similar query in Entity Framework

c# duplicates entity-framework sql-server-2008

Question

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>();
1
15
2/14/2011 6:24:49 PM

Accepted Answer

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.

20
7/19/2012 8:22:44 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow