实体框架查询结果重复 [英] Entity Framework Query Results Duplicate

查看:189
本文介绍了实体框架查询结果重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我创建了一个连接几个表的SQL视图,当在SQL
    中测试时,Manager会提供正确的数据(如果这是复杂的连接有差异)。 >
  • 在MVC中,我创建了一个(数据优先)实体数据模型,然后添加了代码
    生成。

  • 我在控制器中有一个LINQ查询生成一个idex页面。



所有基本的MVC内容。



当我运行应用程序我收到重复的记录。要进一步限定,如果一个订单有多条线,它将返回订单中每一行的第一条记录。

  SQL结果
订单,行,部分
12345,1,3829138120
12345, 2,1238401890

MVC / EF结果
订单,
12345,1,3829138120
12345,1,3829138120

解决方案>

您应该重复检查实体键属性。我看到你的查询结果,你应该标记为主键Order和Line,我认为你已经忘记了标记顺序的关键。


  • I created a SQL View that joins a few tables and when tested in SQL Manager it provides the correct data (If a makes a difference these are complex joins).
  • In MVC I created a (data first) Entity Data Model and then added code generation.
  • I have a LINQ query in a controller that generates an idex page.

All basic MVC stuff.

When I run the application I receive duplicate records. To further qualify, if an order has more than one line it returns the first record for each line in the "order".

SQL Results
Order, Line, Part
12345, 1, 3829138120
12345, 2, 1238401890

MVC/EF Results
Order, Line
12345, 1, 3829138120
12345, 1, 3829138120

Any thoughts as to the cause of this problem?

解决方案

You should double check the entity key attributes. I see your query result and you should mark as primary key Order and Line and I think that you have forget mark order as key.

这篇关于实体框架查询结果重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆