【问题标题】:MVC Code- or Model FirstMVC 代码或模型优先
【发布时间】:2013-10-30 08:42:32
【问题描述】:

大多数 MVC 与实体框架的教程都以 Code-First 为中心,您可以在其中编写用于生成模型的类。这提供了控制和迁移的优势,但我认为它缺乏概述。因此,我更愿意使用图形设计器创建模型,但我看不到数据迁移在这种情况下如何或是否工作。看来,当我更改模型(数据库中的数据)时,所有表中的所有数据都被删除。

  1. 有没有办法解决这个问题?
  2. 使用 Model-First 时如何进行验证?部分课程?

【问题讨论】:

    标签: asp.net-mvc entity-framework validation entity-framework-migrations ef-model-first


    【解决方案1】:

    你可以在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2011-08-13
      相关资源
      最近更新 更多