简体   繁体   English

如何在 ASP.NET MVC 中使用 EntityFramework6.Npgsql 从 Postgres 数据库获取数据?

[英]how to get data from Postgres data base with EntityFramework6.Npgsql in ASP.NET MVC?

I am creating a login application in ASP.NET MVC using Npgsql and EntityFramework6.Npgsql, I want to implement a function to retrieve the password through an identification number, that is, the user clicks the button "I forgot my password", then he must type the identification number and the program checks if it exists or is correct, after that, the user information to change the password is displayed.我正在使用Npgsql和EntityFramework6.Npgsql在ASP.NET MVC中创建一个登录应用程序,我想实现一个通过标识号找回密码的功能,即用户点击“我忘记了密码”按钮,然后他必须输入识别号,程序检查它是否存在或正确,然后显示更改密码的用户信息。

The model class for user:用户的模型类:

public partial class user
{

    public int id { get; set; }
    [Display(Name = "Id number")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary the id number")]
    public string idnumber { get; set; }

    [Display(Name = "Name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary the name")]
    public string fname { get; set; }

    [Display(Name = "Last name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary the last name)]
    public string lname { get; set; }

    [Display(Name = "Password")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary a pass")]
    [DataType(DataType.Password)]
    [MinLength(6, ErrorMessage = "Min six letters")]
    public string pass { get; set; }

    [Display(Name = "Confirm pass")]
    [DataType(DataType.Password)]
    [Compare("pass", ErrorMessage = "The pass don't match")]
    public string check_pass { get; set; }

    [Display(Name = "Email")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary an email")]
    [DataType(DataType.EmailAddress)]
    public string email { get; set; }
}

So, in the [HttpGet] the identification number is obtained by the program and the [HttpPost] the information is changed by the identification number, or so it is supposed to do.所以,在[HttpGet]中标识号是由程序获取的,而在[HttpPost]中信息是由标识号改变的,或者它应该这样做。

[HttpGet]
    public ActionResult recoverPass()
    {
        return View();
    }

    [HttpPost]
    public ActionResult recoverPass(userPassRecover user)
    {    
        using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
        {
            var u = dc.user.Where(a => a.cedula == user.cedula).FirstOrDefault();

            dc.user.Remove(u);
            dc.user.Add(user);
            dc.SaveChanges();

            return View();
        }
    }

I really don't know how to make this work, any suggestions are appreciated我真的不知道如何使这项工作,任何建议表示赞赏

If anyone has the same problem:如果有人遇到同样的问题:

    [HttpGet]
    public ActionResult recoverPass()
    {

        return View();
    }

    [HttpPost]
    public ActionResult recoverPass(userRecoverPass user)
    {
        using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
        {
            var u = dc.user.Where(a => a.idnumber == user.idnumber)?.FirstOrDefault();

            if (u != null)
            {
                u.pass = user.newpass;
                dc.SaveChanges();
            }

            return RedirectToAction("login");
        }
    }

Where the ʻuserRecoverPass` object is the model where only the information is requested to perform the password change.其中 ʻuserRecoverPass` 对象是仅请求信息来执行密码更改的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM