仅插入数据库字段中的值,以 EF ASP.NET



我是EntityFramework的新手,也是 ASP.Net

我正在使用 EF 在 MVC5 中创建 Asp.net 身份验证系统,并且我的数据库中有以下字段

Id | Name | Contact | Url | Username | Password

我的模型Clients.cs,其中包含需要存储在数据库中的类Clients

public class Clients
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public long? Contact { get; set; }
    [Required]
    [Display(Name = "Site Url")]
    public string Url { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Required]
    [Compare("Password", ErrorMessage = "Password Mismatched. Re-enter your password")]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm Password")]
    public string ConfirmPassword { get; set; }
}

我尝试存储凭据,如下所示:

//Controller::
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(Clients clients)
    {
        if (ModelState.IsValid)
        {
            Clients client = new Clients();
            client.Name = clients.Name;
            client.Password = clients.Password;
            client.Url = clients.Url;
            client.Username = clients.Username;
            client.Contact = clients.Contact;
            dbContext.Clients.Add(client);
            dbContext.SaveChanges();
            return RedirectToAction("api/products");
        }
        return View();
    }

它显示一个错误:

Server Error in '/ProductsApp' Application.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Source Error:

Line 32: 
Line 33:                 dbContext.Clients.Add(client);
Line 34:                 dbContext.SaveChanges();
Line 35:                 return RedirectToAction("Index");
Line 36:             }

如何将除字段以外的所有字段存储在数据库中ConfirmPassword字段。

使用NotMappedAttribute

[NotMapped]
public string MyProperty { get; set; }

您的财产将成为:

[Required]
[Compare("Password", ErrorMessage = "Password Mismatched. Re-enter your password")]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[NotMapped]
public string ConfirmPassword { get; set; }

我在Marthijn的帮助下解决了这个问题

我将客户端确认密码更改为:

[Compare("Password", ErrorMessage = "Password Mismatched. Re-enter your password")]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[NotMapped]
public string ConfirmPassword { get; set; }

和控制器的代码为:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Clients clients)
{
    if (ModelState.IsValid)
    {
        dbContext.Clients.Add(client);
        dbContext.SaveChanges();
        return RedirectToAction("Index");
    }
    return View();
}

它奏效了....感谢马蒂恩

最新更新