Introduction
Sometimes while using SQL we experience that SQL query returns same or duplicate results. Since we expect and need a single result from the query, it is a problem. Here we will discuss what causes duplicate records and how to avoid them in SQL view using Entity Framework.You can also know about how to Fix SQL error: 26 - A network-related error occurred in SQL Server by reading my previous article.
Duplicate rows in Entity Framework SQL View
In Entity Framework primary key is known as entity key. 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.The problem is on the application side Entity framework mapping, as object mapping rules in Entity framework are different with database. The Entity framework does the mapping on application memory with accordance to its designed logic when the query results returns from database. Entity Model has a unique way of handling rows with equal primary key values. To fix issue duplicate records in SQL view using Entity Framework, you need to write below code:
You can set AsNoTracking option directly on your view to resolve this issue.
context.myviewname.AsNoTracking().Where(x => x.ColumnName != null);
Post A Comment:
0 comments: