Beispiel #1
0
 // Since EF knows to look for a connection string with the same name as the context,
 // we can specify the connection string in our application config file (app.config)
 public int AddStudent(string studentName)
 {
     // Instead of opening a connection, we just initialize the context
     using (var context = new SchoolContext())
     {
         // Lets create a new Student entity to add
         Student student = new Student() { Name = studentName };
         // Adding a new Student object to the SchoolContext.Students collection creates a new record
         context.Students.Add(student);
         // Our new record isn't actually saved to the database until SchoolContext.SaveChanges is called
         return context.SaveChanges();
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("hello world. This is Entity framework.");

            using (var ctx = new SchoolContext())
            {

             //  ctx.Database.Initialize(false);

                Student s = new Student();

                Console.Write("Enter Student Details");
                Console.Write("Name: ");
                s.Name = Console.ReadLine().ToString();

                Console.Write("Phone: ");
                s.Phone = Console.ReadLine().ToString();

                Console.Write("Address: ");
                s.Address = Console.ReadLine().ToString();

                Console.Write("Course: ");
                s.Course = Console.ReadLine().ToString();

                Console.Write("Description: ");
                s.Description = Console.ReadLine().ToString();

                s.DateOfBirth= new DateTime(1990,12,01);
                ctx.Students.Add(s);
                ctx.SaveChanges();

                var query = from st in ctx.Students
                    orderby st.Name
                    select st;

                Console.WriteLine("Students in Student context");

                foreach (var item in query)
                {
                    Console.WriteLine(item.Name);
                }

                Console.ReadLine();
            }
        }