Asp.net mvc 仅插入ASP.NET中数据库字段中带有EF的值

Asp.net mvc 仅插入ASP.NET中数据库字段中带有EF的值,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我是EntityFramework和ASP.Net的新手 我正在用EF在Asp.net MVC5中创建一个身份验证系统,我的数据库中有以下字段 Id | Name | Contact | Url | Username | Password 我有一个需要存储在数据库中的类Clients的模型Clients.cs: public class Clients { public int Id { get; set; } [Required] public string Nam

我是EntityFramework和ASP.Net的新手

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

Id | Name | Contact | Url | Username | Password
我有一个需要存储在数据库中的类
Clients
的模型
Clients.cs

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字段以外的所有字段。

使用:

您的财产将成为:

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

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

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

[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();
}
它成功了。。。。
多亏了

我是否需要删除
[必需]
不,您也可以(而且应该)使用其他属性(我在回答中没有复制粘贴它们)。我更新了我的答案,使它更清楚一点。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Clients clients)
{
    if (ModelState.IsValid)
    {
        dbContext.Clients.Add(client);
        dbContext.SaveChanges();
        return RedirectToAction("Index");
    }
    return View();
}