Beispiel #1
0
        private static void AddTagUsingAttributeValidation(PhotographersContext context)
        {
            /* Using TagTransform with TagAttribute Validations.
             * To test this method =>
             * Uncomment (Enable) the [Tag] annotation in Tag.cs &
             * Comment (Disdable) the respective anotated lines of code in Tag.cs */

            Console.WriteLine("Solution to Problem 8:\n" +
                              "* Using TagTransform with attribute validation.");
            string tagName = String.Empty;

            while (true)
            {
                Console.Write("Enter Tag Name: ");
                tagName = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(tagName))
                {
                    break;
                }
                Console.Write("Tag Name cannot be empty. ");
            }
            Tag tag = new Tag()
            {
                Name = tagName
            };

            context.Tags.Add(tag);

            try
            {
                context.SaveChanges();
                Console.WriteLine($"{tag.Name} was added to the database");
            }
            catch (DbEntityValidationException e)
            {
                tag.Name = TagTransformer.Transform(tag.Name);
                if (context.Tags.FirstOrDefault(t => t.Name == tag.Name) == null)
                {
                    context.SaveChanges();
                    Console.WriteLine($"{tag.Name} was added to the database");
                }
                else
                {
                    Console.WriteLine($"{tag.Name} already exists in the database");
                }
            }
        }
Beispiel #2
0
        private static void AddTagUsingTagTransform(PhotographersContext context)
        {
            /* Using TagTransform in the Setter, no atttibute validations.
             * To test this method =>
             * Comment (Disable) the [Tag] annotation in Tag.cs &
             * Uncomment (Enable) the respective anotated lines of code in Tag.cs */

            Console.WriteLine("Solution to Problem 8:\n" +
                              "* Using TagTransform in the setter, no atttibute validation.\n" +
                              "* To test TagTransform with attribute validation, follow the comments.");
            string tagName = String.Empty;

            while (true)
            {
                Console.Write("Enter Tag Name: ");
                tagName = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(tagName))
                {
                    break;
                }
                Console.Write("Tag Name cannot be empty. ");
            }
            Tag tag = new Tag()
            {
                Name = tagName
            };                                      // a valid tag is set in the setter

            if (context.Tags.FirstOrDefault(t => t.Name == tag.Name) == null)
            {
                context.Tags.Add(tag);
                context.SaveChanges();
                Console.WriteLine($"{tag.Name} was added to the database");
            }
            else
            {
                Console.WriteLine($"{tag.Name} already exists in the database");
            }
        }