我的EF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace myEF
{
    class Program
    {
        public static void add()
        {
            using (var db = new Entities())
            {
                Console.WriteLine("请输入博客名称:");
                var name = Console.ReadLine();
                var blog = new myBlogSet { myblogs = name };
                db.myBlogSet.Add(blog);
                db.SaveChanges();
            }
        }

        public static void list()
        {
            using (var db = new Entities())
            {
                var query = from b in db.myBlogSet orderby b.Id select b;
                Console.WriteLine("当前博客信息为:");
                foreach (var item in query)
                {

                    Console.WriteLine("编号:" + item.Id + " 标题:" + item.myblogs);
                }
            }
        }

        public static void search()
        {
            using (var db = new Entities())
            {
                Console.WriteLine("请输入需要删除的编号:");
                int inputID = Convert.ToInt32(Console.ReadLine());
                var query = from b in db.myBlogSet orderby b.Id select b;
                Console.WriteLine("查询出博客信息为:");
                foreach (var item in query.Where(b => b.Id == inputID))
                {

                    Console.WriteLine("编号:" + item.Id + " 标题:" + item.myblogs);
                }
            }
        }

        public static void del()
        {
            Console.WriteLine("请输入需要删除的编号:");
            int inputID = Convert.ToInt32(Console.ReadLine());
            using (var db = new Entities())
            {
                myBlogSet myfind = db.myBlogSet.Find(inputID);
                db.myBlogSet.Remove(myfind);
                db.SaveChanges();

            }

        }

        public static void update()
        {
            Console.WriteLine("请输入需要删除的编号:");
            int inputID = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入内容:");
            string Name = Console.ReadLine();
            using (var db = new Entities())
            {
                myBlogSet myfind = db.myBlogSet.Find(inputID);
                myfind.myblogs = Name;
                db.SaveChanges();
            }
        }

        static void Main(string[] args)
        {
            string readInput = "";
            while (readInput != "q" && readInput != "Q")
            {
                Console.WriteLine("请选择菜单:");
                Console.WriteLine("1、列出所有信息:");
                Console.WriteLine("2、删除信息:");
                Console.WriteLine("3、编辑信息:");
                Console.WriteLine("4、新建信息:");
                Console.WriteLine("5、查询信息:");
                Console.WriteLine("输入Q退出");
                readInput = Console.ReadLine();
                switch (readInput)
                {
                    case "1":
                        list();
                        break;
                    case "2":
                        del();
                        break;
                    case "3":
                        update();
                        break;
                    case "4":
                        add();
                        break;
                    case "5":
                        search();
                        break;
                }
                Console.WriteLine("按任意键继续......");
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}

 

posted @ 2013-11-08 17:14  王道-旭忠著  阅读(128)  评论(0编辑  收藏  举报