2014-01-17 34 views
0

我是新來的EntityFramework也ASP.Net插入只有那些在數據庫領域的ASP.NET與EF

我在Asp.net MVC5創建一個身份驗證系統與EF和I值有在我的分貝以下領域

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場。

回答

0

我解決了這個問題隨着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(); 
} 

和它的工作.... 由於Marthijn

1

使用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; } 
+0

做我需要刪除'[必需]' – Robz

+0

不,你可以(也應該)使用其他屬性(我沒有複製粘貼在我的答案中)。我更新了我的答案,所以它更清晰一點。 – Marthijn