【问题标题】:DBSet does not contain a definition for Where [duplicate]DBSet 不包含 Where [重复] 的定义
【发布时间】:2014-11-10 16:17:08
【问题描述】:

当尝试从模型对我的数据库上下文执行 .Where() 时,我收到以下错误消息:

System.Data.Entity<RPSManagementSystem.Model.StoreUser> does not contain a definition for Where...

这在从控制器调用时有效。什么给了?

来自模型:

[NotMapped]
private List<StoreUser> _stores { get; set; }
[NotMapped]
public List<StoreUser> Stores
{
    get
    {
        if (this._stores == null || this._stores.Count <= 0)
        {
            using (RPSEntities db = new RPSEntities())
            {
                this._stores = db.StoreUsers.Where(su => su.Username == this.Username);
            }
        }

        return _stores;
    }
}    

为了确保我没有发疯,我将它粘贴到我的控制器中 - 它看起来可以正常工作。截图如下:

在模型中:

在控制器中:

【问题讨论】:

  • 你添加using System.Linq;了吗?
  • @Alberto facepalm 你成功了。您可以将此作为答案发布,以便我将其标记为已接受吗?

标签: c# asp.net entity-framework


【解决方案1】:

在你的模型类中添加using System.Linq;

【讨论】:

  • 不要吹毛求疵,但我认为 OP 在模型内部遇到问题,而不是控制器。 OP 声明它在控制器中工作。 :-)
  • 我注意到有必要在任何使用 LINQ 功能的类中添加using System.Linq;
猜你喜欢
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多