Incredibly similar query in Entity Framework

c# duplicates entity-framework sql-server-2008

Question

A specific view responds to my SQL query with three distinct rows.

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

However, if I use my entity framework to execute the same query, I receive 3 identical rows in place of the third row, giving me 3 rows total.

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

Please assist me in solving this problem.

EDIT: This is how I updated my query, and it now works. However, because I need to iterate once more for further processing, I still want to return to the one seen above.

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

A similar problem of mine was resolved by altering the ObjectSet's merge option. Example:

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

It seems like views with multiple primary keys or no primary keys at all are handled incorrectly by entity framework (EF). Therefore, if there are two rows that EF believes to be equal, it will load the first row as it should but not the second row since it will assume the first row has already been loaded.

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

Popular Answer

To assist you optimize the speed of your applications, Entity Framework offers a variety of performance tweaking options. This tuning method is one of them. AsNoTracking(). You may instruct Entity Framework not to track a query's results using this optimization. This indicates that Entity Framework doesn't process or store the entities that the query returns in any further ways. But it also implies that updating these things requires reattaching them to the tracking graph.

To fix this problem, you may set the AsNoTracking option directly on your view.

context.viewname.AsNoTracking(). If x.ColumnName!= null, then



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