我使用以下代码:
List<vw_GetIMeasurements> Imeasurements = context.vw_GetIMeasurements.Where(f => f.MEMBERID == userID).Distinct().ToList();
这将返回一个包含 12 个值的列表,但这些值始终相同(即使它们不在数据库中。(即使它们不同也应该解决这个问题)
D B :
返回的原始值也是错误的。这个问题的原因是什么,我该如何解决?
我以前遇到过这个问题 - 原来视图没有唯一entity keys
集 - 确保由实体框架自动创建的视图的主键确实是唯一的......
I had a similar issue - the database view looked fine in SQL server but when debugging, duplicate rows were appearing where rows shared a particular attribute value.
I found that one of the important unique keys was from a outer join table - therefore had the potential to be null - and the model was not assigning as unique.
I was able to fix this problem by setting this field to ISNULL(field, '') - ensuring that a value is always returned - and upon updating, the model correctly interprets the field as unique and as part of a compound key and the rows returned correctly.
I know that one might argue the view design is at fault (i.e. I should not be using an outer join), but in this instance, there was a specific need for the outer join. The fix works because the compound key is still unique even if "" is returned multiple times.
也许尝试像这样更简单的语法
var measure = from f in context.vw_GetIMeasurements
where f.MEMBERID == userID
select f;
这种事情对我有用..
这个问题实际上就是理查德所描述的。但是我无法选择正确的“唯一密钥”,所以这导致了更多问题。
我通过创建一个定制的类并在 SQL 中创建它来解决我的问题。我还将我的选择限制为 3 列中的 2 列。
List<Measurement> result = (from f in context.vw_GetIMeasurements where f.MEMBERID == userID select new Measurement { Category = f.atcmCATEGORYCODEID, Size = f.imMeasurementNumber }).ToList();
测量是在自制类的情况下。