0

Most of the tutorials for MVC with Entity Framework are centered around Code-First, where you write classes for generating the model. This gives the advantage of control and Migrations, but I think it lack overview. I would therefore prefer to create the model using the graphical designer, but I cannot see how or if data migrations work in this context. It seems, that when I change the model (with data in the database), all the data is deleted in all tables.

  1. Is there a way around this?
  2. How can I do validation when using Model-First? Partial classes?
Chuckels5584
  • 551
  • 2
  • 6
  • 17

1 Answers1

0

you may use the global validation beside mvc validation example :

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;
    }



}

Meanwhile you may use validation part in your controller Example :

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