Beispiel #1
0
        public void ValidateBeforeSave()
        {
            // Slouží k tomu objekt Validator

            Author author = new Author
            {
                FirstName = "Miroslav",
                LastName = null,
                Email = null,
                SecondEmail = ""
            };

            ValidationContext validationContext = new ValidationContext(author);

            var results = new List<ValidationResult>();

            var isValid = Validator.TryValidateObject(author, validationContext, results);

            if (!isValid)
            {
                foreach (var validationResult in results)
                {
                    Console.WriteLine(validationResult.ErrorMessage);
                }
            }
        }
Beispiel #2
0
        public void EmailValidation()
        {
            using (var db = new MyContext())
            {
                Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());

                Author author = new Author
                {
                    FirstName = "Miroslav",
                    LastName = "Holec",
                    Email = "*****@*****.**",
                    SecondEmail = "thisIsNotEmail"
                };

                db.Authors.Add(author);
                db.SaveChanges();
            }
        }