C# 无法使用实体框架在ASP.NET中创建登录名
我试图用实体框架做一个注册屏幕,我不使用数据库。使用我的模型的上下文 我正在学习如何使用数据库进行此操作的教程,当我从控制器创建视图时,会出现以下错误: 登录控制器:C# 无法使用实体框架在ASP.NET中创建登录名,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我试图用实体框架做一个注册屏幕,我不使用数据库。使用我的模型的上下文 我正在学习如何使用数据库进行此操作的教程,当我从控制器创建视图时,会出现以下错误: 登录控制器: public class loginController : Controller { private loginContext db = new loginContext(); // GET: login public ActionResult Index() { return
public class loginController : Controller
{
private loginContext db = new loginContext();
// GET: login
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Index(login lc)
{
string mainconn = ConfigurationManager.ConnectionStrings["loginContext"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "select email,password from logins where email=@email and password=@password";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
sqlcomm.Parameters.AddWithValue("@email", lc.email);
sqlcomm.Parameters.AddWithValue("@password", lc.password);
SqlDataReader sdr = sqlcomm.ExecuteReader();
if (sdr.Read())
{
FormsAuthentication.SetAuthCookie(lc.email, true);
Session["emailid"] = lc.email.ToString();
return RedirectToAction("Welcome", "Login");
}
else
{
ViewData["mensage"] = "Username & Password are wrong:";
}
sqlconn.Close();
return View();
}
public ActionResult welcome()
{
return View();
}
}
模型登录:
public class login
{
[Required(ErrorMessage ="Please enter your email")]
[Display(Name ="Enter email:")]
public string email { get; set; }
[Required(ErrorMessage = "Please enter your Password")]
[Display(Name = "Enter Password:")]
[DataType(DataType.Password)]
public string password { get; set; }
}
EntityTypelogin
未定义键。定义此EntityType的键
显然,您的模型没有密钥属性,请更改login
模型,如下所示:
public class login
{
[Key]
public int Id {get; set;}
[Required(ErrorMessage ="Please enter your email")]
[Display(Name ="Enter email:")]
public string email { get; set; }
[Required(ErrorMessage = "Please enter your Password")]
[Display(Name = "Enter Password:")]
[DataType(DataType.Password)]
public string password { get; set; }
}
这个错误将会解决。
祝你好运