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>();
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.
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.
Answer: The DISTINCT keyword is used to remove the duplicate rows in a table.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With