0

大多數有關實體框架的MVC教程均以Code-First爲中心,您可以在其中編寫用於生成模型的類。這提供了控制和遷移的優勢,但我認爲它缺乏概述。因此,我寧願使用圖形設計器來創建模型,但我無法看到數據遷移在這種情況下是如何工作的或者數據遷移工作的。看來,當我更改模型(使用數據庫中的數據)時,所有表中的所有數據都將被刪除。MVC代碼或模型優先

  1. 有沒有辦法解決這個問題?
  2. 如何在使用Model-First時進行驗證?部分班級?

回答

0

您可以使用全局驗證MVC驗證旁邊 例如:

public class ValidationCriteria 
{ 
    public ValidType Type { get; set; } 
    public ValidRange Range { get; set; } 
    public ValidFormat Format { get; set; } 
    public ValidIsNull IsNull { get; set; } 
    public ValidCompare Compare { get; set; } 
    public ValidDB DB { get; set; } 
    public string Trigger { get; set; } 
    public Dictionary<string, ValidationCriteria> Before { get; set; } 
    public string After { get; set; } 


    public class ValidDB 
    { 
     public string functionName { get; set; } 
     public object[] param { get; set; } 
     public object functionClass { get; set; } 
     public string msg { get; set; } 
     public bool check = false; 
    } 
    public class ValidCompare 
    { 
     public string first { get; set; } 
     public string second { get; set; } 
     public string compareOperator { get; set; } 
     public string compareValue { get; set; } 
     public string msg { get; set; } 
     public bool check = false; 


    } 
    public ValidationCriteria() 
    { 
     this.Range = new ValidRange(); 
     this.Format = new ValidFormat(); 
     this.IsNull = new ValidIsNull(); 
     this.Type = new ValidType(); 
     this.Compare = new ValidCompare(); 
     this.DB = new ValidDB(); 

     this.Trigger = "blur"; 
     this.Before = new Dictionary<string, ValidationCriteria>(); 
     this.After = ""; 
    } 
    public class ValidType 
    { 
     // checking element is integer. 
     public bool isInt { get; set; } 
     // checking element is decimal. 
     public bool isDecimal { get; set; } 
     public string msg { get; set; } 
     public bool check = false; 
    } 
    public class ValidRange 
    { 
     public long min { get; set; } 
     public long max { get; set; } 
     public string msg { get; set; } 
     public bool check = false; 
    } 
    public class ValidFormat 
    { 
     public bool isEmail { get; set; } 
     public string regex { get; set; } 
     public string msg { get; set; } 
     public bool check = false; 
    } 

    public class ValidIsNull 
    { 
     public string nullDefaultVal { get; set; } 
     public string msg { get; set; } 
     public bool check = false; 
    } 



} 

同時你可能在你的控制器使用驗證部分 例子:

private bool validateMaintainanceManagement(MaintainanceCRUD.Maintainance model, bool edit = false, bool ServerValidation = true) 
    { 
     bool ValidModel = false; 

     Dictionary<string, ValidationCriteria> validCriteria = new Dictionary<string, ValidationCriteria>(); 

     #region maintainTitle Criteria 

     ValidationCriteria maintainTitle = new ValidationCriteria(); 

     maintainTitle.IsNull.msg = Resources.Home.ErrmaintainTitle; 
     maintainTitle.IsNull.check = true; 
     maintainTitle.IsNull.nullDefaultVal = "-1"; 

     //maintainTitle.Trigger = "change"; // this may trigger if you are using dropdown 
     validCriteria.Add("maintainTitle", maintainTitle); 
     #endregion