コード例 #1
0
ファイル: DataAccessMethods.cs プロジェクト: gaines/nss
 public Student GetStudent(int studentId)
 {
     // Since EF will give us a Student object, lets return it
     Student student = null;
     // Instead of opening a connection, we just initialize the context
     using (var context = new SchoolContext())
     {
         // We can use LINQ to get the first student that has a matching Id
         student = context.Students.First(s => s.Id == studentId);
     }
     return student;
 }
コード例 #2
0
ファイル: DataAccessMethods.cs プロジェクト: gaines/nss
 public List<Student> GetStudentList()
 {
     // Since EF will give us a list of Students, lets return it
     List<Student> studentList;
     // Instead of opening a connection, we just initialize the context
     using (var context = new SchoolContext())
     {
         // We can use the ToList method to return an entire list of students
         studentList = context.Students.ToList();
     }
     return studentList;
 }
コード例 #3
0
ファイル: DataAccessMethods.cs プロジェクト: gaines/nss
 // 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();
     }
 }
コード例 #4
0
        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();
            }
        }