Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC代码或模型优先_Asp.net Mvc_Entity Framework_Validation_Entity Framework Migrations_Ef Model First - Fatal编程技术网

Asp.net mvc MVC代码或模型优先

Asp.net mvc MVC代码或模型优先,asp.net-mvc,entity-framework,validation,entity-framework-migrations,ef-model-first,Asp.net Mvc,Entity Framework,Validation,Entity Framework Migrations,Ef Model First,大多数针对MVC with Entity Framework的教程都以代码优先为中心,您可以在其中编写用于生成模型的类。这提供了控制和迁移的优势,但我认为它缺乏概述。因此,我更愿意使用图形设计器创建模型,但我看不到数据迁移在这种环境下是如何工作的或是否工作的。看起来,当我更改模型(数据库中有数据)时,所有表中的所有数据都会被删除 有办法解决这个问题吗 首先使用模型时如何进行验证?部分课程 您可以在mvc验证旁边使用全局验证 例如: public class ValidationCriteria

大多数针对MVC with Entity Framework的教程都以代码优先为中心,您可以在其中编写用于生成模型的类。这提供了控制和迁移的优势,但我认为它缺乏概述。因此,我更愿意使用图形设计器创建模型,但我看不到数据迁移在这种环境下是如何工作的或是否工作的。看起来,当我更改模型(数据库中有数据)时,所有表中的所有数据都会被删除

  • 有办法解决这个问题吗
  • 首先使用模型时如何进行验证?部分课程

  • 您可以在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;
        }
    
    
    
    }
    
    公共类验证标准
    {
    公共有效类型类型{get;set;}
    公共ValidRange范围{get;set;}
    公共有效格式{get;set;}
    公共ValidIsNull为空{get;set;}
    公共有效比较{get;set;}
    公共有效数据库{get;set;}
    公共字符串触发器{get;set;}
    {get;set;}之前的公共字典
    {get;set;}之后的公共字符串
    公共类有效
    {
    公共字符串函数名{get;set;}
    公共对象[]参数{get;set;}
    公共对象函数类{get;set;}
    公共字符串msg{get;set;}
    公共布尔检查=假;
    }
    公共类有效比较
    {
    第一个公共字符串{get;set;}
    公共字符串第二个{get;set;}
    公共字符串比较器{get;set;}
    公共字符串compareValue{get;set;}
    公共字符串msg{get;set;}
    公共布尔检查=假;
    }
    公共验证标准()
    {
    this.Range=new ValidRange();
    this.Format=新的ValidFormat();
    this.IsNull=new ValidIsNull();
    this.Type=新的ValidType();
    this.Compare=new ValidCompare();
    this.DB=新的ValidDB();
    this.Trigger=“blur”;
    this.Before=新字典();
    此。在“”之后;
    }
    公共类有效类型
    {
    //检查元素是否为整数。
    公共布尔值为{get;set;}
    //校验元素是十进制的。
    公共bool是特定的{get;set;}
    公共字符串msg{get;set;}
    公共布尔检查=假;
    }
    公共类ValidRange
    {
    公共长min{get;set;}
    公共长最大值{get;set;}
    公共字符串msg{get;set;}
    公共布尔检查=假;
    }
    公共类有效格式
    {
    公共布尔isEmail{get;set;}
    公共字符串正则表达式{get;set;}
    公共字符串msg{get;set;}
    公共布尔检查=假;
    }
    公共类ValidIsNull
    {
    公共字符串nullDefaultVal{get;set;}
    公共字符串msg{get;set;}
    公共布尔检查=假;
    }
    }
    
    同时,您可以在控制器中使用验证部分 例如:

    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
    
    private bool validateMaintainanceManagement(maintanceCrud.maintanceModel,bool edit=false,bool ServerValidation=true)
    {
    bool-ValidModel=false;
    Dictionary validCriteria=新字典();
    #地区名称标准
    ValidationCriteria MaintaintTitle=新的ValidationCriteria();
    maintaintTitle.IsNull.msg=Resources.Home.errmaintaintTitle;
    maintaintTitle.IsNull.check=true;
    maintaintTitle.IsNull.nullDefaultVal=“-1”;
    //maintaintTitle.Trigger=“change”//如果使用下拉列表,则可能会触发此操作
    添加(“维护标题”,维护标题);
    #端区