Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

A Beginner's Tutorial on Performing Validations in Entity Framework using Partial Methods

Rate me:
Please Sign up or sign in to vote.
4.70/5 (8 votes)
6 Oct 2012CPOL4 min read 35.4K   745   33  
This article talks about performing validations while using Entity framework. We will see how the Entities facilitate validations using partial methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace EntityFrameworkTestCosnsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string choice = null;
            while (true)
            {
                try
                {
                    // Lets ask the user what he wants to do
                    Console.WriteLine("\nSelect the operation you want to perform");
                    Console.WriteLine("1. Select\n2. Add\n3. Update\n4. Delete\nX. To exit");

                    choice = Console.ReadLine();

                    // If the user want to exit, then let him
                    if (string.Equals(choice, "X", StringComparison.OrdinalIgnoreCase) == true)
                    {
                        break;
                    }

                    // So the user want to perform some operations, let him do it
                    switch (choice)
                    {
                        case "1":
                            using (MotorBikeShopDbEntities db = new MotorBikeShopDbEntities())
                            {
                                List<Bike> bikes = db.Bikes.ToList<Bike>();

                                Console.WriteLine("BikeID | BikeName | Manufacturer | Modelumber");
                                foreach (Bike b in bikes)
                                {
                                    Console.WriteLine(b.BikeID + " | " + b.BikeName + " | " + b.Manufacturer + " | " + b.Modelumber);
                                }
                            }
                            break;

                        case "2":
                            Bike bike = new Bike();
                            Console.WriteLine("Enter Bike Name");
                            bike.BikeName = Console.ReadLine();
                            Console.WriteLine("Enter Bike Manufacturer");
                            bike.Manufacturer = Console.ReadLine();
                            Console.WriteLine("Enter Bike ModelNumber");
                            bike.Modelumber = Console.ReadLine();

                            using (MotorBikeShopDbEntities db = new MotorBikeShopDbEntities())
                            {
                                db.Bikes.AddObject(bike);
                                db.SaveChanges();
                            }

                            Console.WriteLine("A new Bike has been added in the DB");

                            break;
                        case "3":
                            Console.WriteLine("Select ID of Bike to update");
                            int bikeID;
                            if (int.TryParse(Console.ReadLine(), out bikeID) == true)
                            {

                                using (MotorBikeShopDbEntities db = new MotorBikeShopDbEntities())
                                {
                                    Bike bikeToUpdate = db.Bikes.SingleOrDefault<Bike>(p => p.BikeID == bikeID);
                                    if (bikeToUpdate != null)
                                    {
                                        Console.WriteLine("Enter new manufacturer name.");
                                        string newManf = Console.ReadLine();

                                        bikeToUpdate.Manufacturer = newManf;
                                        if (db.SaveChanges() > 0)
                                        {
                                            Console.WriteLine("Updated");
                                        }
                                        else
                                        {
                                            Console.WriteLine("Failed");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("Bike with this Id not found");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid ID");
                            }
                            break;
                        case "4":
                            Console.WriteLine("Select ID of Bike to delete");
                            int bikeId;
                            if (int.TryParse(Console.ReadLine(), out bikeID) == true)
                            {
                                using (MotorBikeShopDbEntities db = new MotorBikeShopDbEntities())
                                {
                                    Bike bikeToDelete = db.Bikes.SingleOrDefault<Bike>(p => p.BikeID == bikeID);
                                    if (bikeToDelete != null)
                                    {
                                        db.DeleteObject(bikeToDelete);

                                        Console.WriteLine("Bike Deleted");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Bike with this Id not found");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid ID");
                            }
                            break;
                        default:
                            Console.WriteLine("Invalid Operation");
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }            
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions