Linq To Entities using a view is returning duplicated values
I am using Linq over a View in an SQL Server Database. The concrete sentence is the following:
var valoresFiltrados = Context.VDoNotUse_v2.Select(doNotUse => doNotUse).Where(doNotUse => doNotUse.PlatformValue.Equals(platform) && doNotUse.Bank.Equals(bank) && doNotUse.LanguageValue.Equals("CZE_CZ")).OrderBy(doNotUse => doNotUse.ID);
where VDoNotUse is the View's Entity. This sentece is returning duplicated values (the first IDs are for example 12170, 12171, 12170, 12171, 12204...) and those values are not ordered, as you can see in the example. However, if I use an SqlDataAdapter with the following sentence (which I am sure is the equivalent to the Linq one), it works and returns the correct values:
"SELECT * FROM VDoNotUse_v2 WH开发者_JAVA技巧ERE PlatformValue = '" + platform + "' AND Bank = '" + bank + "' AND LanguageValue = 'CZE_CZ' order by id asc"
Of course, both of them are using the same connection and DataBase.
Does anyone knows why this is happening?
You need to make sure that you have set the entity keys in your entity datamodel to your view correctly. I find that the default entity keys are usually wrong.
精彩评论