实体框架中的结果与 LINQ to SQL 中的结果不同
我首先在项目中使用 LINQ to SQL,并使用了以下语句:
var ProjectRouteEmails = EmailManagerDAL.Context.ProjectRouteEmails
.Where(p => p.ProjectID == ProjectID);
从 ProjectRouteEmails 视图中正确返回了三封不同的电子邮件。从 Emails 表返回的 ID 是 117、591 和 610。
我更改为 LINQ to Entities 并使用相同的视图和相同的 LINQ 语句,但即使我返回三个记录,它也是第一条记录,ID 117,即被退回三次。
我尝试像这样编写 LINQ 语句:
var ProjectRouteEmails = from p in EmailManagerDAL.Context.ProjectRouteEmails
where p.ProjectID == ProjectID
select p;
但这没有什么区别;同一条记录返回了 3 次。
我进入 SQL Server Management Studio 并运行查询:
select * from ProjectRouteEmails (nolock)
where ProjectID = 12
并返回了正确的三个唯一记录。
这是怎么回事?
谢谢!
I was first using LINQ to SQL in my project and used the following statement:
var ProjectRouteEmails = EmailManagerDAL.Context.ProjectRouteEmails
.Where(p => p.ProjectID == ProjectID);
That correctly returned the three distinct emails from the view ProjectRouteEmails. The IDs returned from the Emails table were 117, 591, and 610.
I changed to LINQ to Entities and use the same view and same LINQ statement, but even though I am getting back three records, it is the first record, ID 117, that is getting returned three times.
I tried writing the LINQ statment like this:
var ProjectRouteEmails = from p in EmailManagerDAL.Context.ProjectRouteEmails
where p.ProjectID == ProjectID
select p;
but it made no difference; the same record returned three times.
I went into SQL Server Management Studio and ran the query:
select * from ProjectRouteEmails (nolock)
where ProjectID = 12
and the correct three, unique records returned.
What is going on here?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保实体数据模型中的 ProjectRouteEmails 的实体键设置正确。有时,当您将视图导入模型时,实体键会变得混乱。
Make sure the entity key is set correctly for ProjectRouteEmails in the Entity Data Model. Sometimes the entity keys are messed up when you import the view into the model.