【发布时间】: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