【问题标题】:How to fix "System.InvalidOperationException"如何修复“System.InvalidOperationException”
【发布时间】:2019-10-17 02:42:50
【问题描述】:

我正在尝试在 ASP.Net 网络应用程序上创建一个登录页面。但是当我单击登录按钮时出现此错误:“System.InvalidOperationException:'实体类型 UserTable 不是当前上下文模型的一部分。”

这里是代码


        public ActionResult Authorise(UserTable user)
        {
            using (DBModels db = new DBModels())
            {
                var userDetail = db.UserTables.Where(x => x.username == user.username && x.password == user.password).FirstOrDefault();
                if(userDetail == null)
                {
                    user.LoginErrorMsg = "Invalid Username or Password";
                    return View("Index", user);
                }
                else
                {
                    Session["id"] = user.id;
                    return RedirectToAction("Index", "Home");
                }
            }

        }

【问题讨论】:

  • 如果你能分享一个minimal reproducible example,那就太棒了。
  • 错误说明 DBModels 类没有 UserTables 属性。在 DBModels 构造函数中创建 Usertables 或单独创建并将其设置为 DBModels db。
  • @JReno 我在 DBModels 数据库中有 UserTable
  • 如果您使用基于 edmx 的配置,请检查您的连接字符串。可能实体连接字符串不同。
  • 如果您将 db 添加到您的 Locals 或 Watch 中,它是否具有 UserTables 属性?

标签: c# sql database authentication model-view-controller


【解决方案1】:

远射:UserTables 和“UserTable”不匹配。

如果你有这种情况会发生

[Table("UserTable")]
public class UserTables

并且数据库中的表没有被称为UserTable

【讨论】:

  • 表名是 UserTable 。数据库名称是 DBModels。我跟着教程,它是用户表,我不知道为什么
  • 数据库中的表名是UserTable
  • 如果您使用用于代码的连接字符串登录到数据库,它是否有UserTable 表?
猜你喜欢
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 2010-11-12
  • 2012-10-23
  • 1970-01-01
相关资源
最近更新 更多