static void Main(string[] args) { StudentsContext context = new StudentsContext(); context.Database.EnsureCreated(); Console.WriteLine("Enter Student full name"); String fullName = Console.ReadLine(); String[] parts = fullName.Split(); if (parts.Length == 2) { Students newStudent = new Students(parts[0], parts[1]); //context.Students.Add(newStudent); context.SaveChanges(); Console.WriteLine("Added the student."); } else { Console.WriteLine("Invalid full name, did not add student"); } Console.WriteLine("The Current List of students are: "); //foreach (Students s in context.Students) //{ // Console.WriteLine("{0} - {1} {2}", // s.Id, s.FirstName, s.LastName); //} }
static void Main(string[] args) { StudentsContext context = new StudentsContext(); context.Database.EnsureCreated(); Console.WriteLine("enter Student full name"); String fullName = Console.ReadLine(); string[] nameParts = fullName.Split(); Student newStudetn = new Student(nameParts[0], nameParts[1]); context.Students.Add(newStudetn); context.SaveChanges(); }
static void Main(string[] args) { // instantiate an instance of the context StudentsContext context = new StudentsContext(); // makes sure that the table exists, //and creates it if it does not already exist context.Database.EnsureCreated(); // ask the user for a student to add Console.WriteLine("Enter Student full name"); String fullName = Console.ReadLine(); // split the input into parts, and make sure // we have 2 parts only String[] parts = fullName.Split(); if (parts.Length == 2) { // create a new student object, notce that we do not // select an id, we let the framework handle that Student newStudent = new Student(parts[0], parts[1]); // add the newly created student instance to the context // notice how similar this is to adding a item to a list, context.students.Add(newStudent); // ask the context to save any changes to the database context.SaveChanges(); Console.WriteLine("Added the student."); } else { Console.WriteLine("Invalid full name, did not add student"); } Console.WriteLine("The Current List of students are: "); // use a for each loop to loop through the students in the context // notice how similar this is to looping through a list foreach (Student s in context.students) { Console.WriteLine("{0} - {1} {2}", s.Id, s.FirstName, s.LastName); } }