コード例 #1
0
        public static void Main(string[] args)
        {
            MyDbEntities db = new MyDbEntities();

            Area newArea = new Area()
            {
                AreaName = "Oxford Street", CityId = 4, Pincode = "123456"
            };

            db.Areas.Add(newArea);
            Console.WriteLine("After Adding");
            foreach (var tracker in db.ChangeTracker.Entries <Area>())
            {
                Console.WriteLine(tracker.State);
            }



            Area modifiedArea = db.Areas.Find(3);

            if (modifiedArea != null)
            {
                modifiedArea.Pincode = "124522";


                Console.WriteLine("After Modification");
                foreach (var tracker in db.ChangeTracker.Entries <Area>())
                {
                    Console.WriteLine(tracker.State);
                }
            }

            Area delArea = db.Areas.Find(1);

            if (delArea != null)
            {
                db.Areas.Remove(delArea);
            }


            Console.WriteLine("After Deletion");
            foreach (var tracker in db.ChangeTracker.Entries <Area>())
            {
                Console.WriteLine(tracker.State);
            }
            Console.ReadLine();
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            MyDbEntities db      = new MyDbEntities();
            Area         areaObj = new Area();

            int opt, id;

            do
            {
                Console.WriteLine("1:New Record\n 2:Display\n3:Update Record \n4:Delete Record\n 5:Exit");
                Console.Write("Enter Your Option");
                opt = Convert.ToInt32(Console.ReadLine());

                switch (opt)
                {
                case 1:    // add new Record
                    Console.WriteLine("Enter Area Name, CityId and Pincode:");
                    areaObj.AreaName = Console.ReadLine();
                    areaObj.CityId   = Convert.ToInt32(Console.ReadLine());
                    areaObj.Pincode  = Console.ReadLine();

                    db.Areas.Add(areaObj);
                    db.SaveChanges();

                    break;

                case 2:    // read all Records

                    foreach (Area a in db.Areas)
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}", a.AreaId, a.AreaName, a.Pincode, a.City.CityName);
                    }
                    break;

                case 3:    // update Records
                    Console.Write("Enter Id to Update:");
                    id      = Convert.ToInt32(Console.ReadLine());
                    areaObj = db.Areas.Find(id);
                    if (areaObj == null)
                    {
                        Console.WriteLine("Invalid Area Id, Try Again");
                    }
                    else
                    {
                        Console.WriteLine("Enter Modified  Area Name, CityId and Pincode:");
                        areaObj.AreaName = Console.ReadLine();
                        areaObj.CityId   = Convert.ToInt32(Console.ReadLine());
                        areaObj.Pincode  = Console.ReadLine();
                        db.SaveChanges();
                    }

                    break;

                case 4:    // delete Records
                    Console.Write("Enter Id to Delete:");
                    id      = Convert.ToInt32(Console.ReadLine());
                    areaObj = db.Areas.Find(id);
                    if (areaObj == null)
                    {
                        Console.WriteLine("Invalid Area Id, Try Again");
                    }
                    else
                    {
                        db.Areas.Remove(areaObj);
                        db.SaveChanges();
                    }
                    break;

                case 5: break;

                default: Console.WriteLine("Invalid Option "); break;
                }
            }while(opt != 5);
        }