protected void SaveButton_Click(object sender, EventArgs e) { //connect to Entity Framework Database using (ContosoContext db = new ContosoContext()) { // create a new student Student newStudent = new Student(); int studentID = 0; //get student info from the form newStudent.LastName = LastNameTextBox.Text; newStudent.FirstMidName = FirstNameTextBox.Text; newStudent.EnrollmentDate = Convert.ToDateTime(EnrollmentDateTextBox.Text); if (studentID == 0) { //add new student db.Students.Add(newStudent); } //save changes db.SaveChanges(); //redirect bk to student page Response.Redirect("~/Contoso/Students.aspx"); } }
public IHttpActionResult PutStudent(Student student) { _context = new ContosoContext(); _context.Entry(student).State = EntityState.Modified; _context.SaveChanges(); return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PostStudent(Student student) { _context = new ContosoContext(); _context.Students.Add(student); _context.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = student.Id }, student)); }
protected void StudentGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get selected row int selectedRow = e.RowIndex; //get selected studentID int StudentID = Convert.ToInt32(StudentGridView.DataKeys[selectedRow].Values["StudentID"]); //use EF & Linq to find the selected student in the DB and remove it using (ContosoContext db = new ContosoContext()) { // create object ot the student clas and store the query inside of it Student deletedStudent = (from studentRecords in db.Students where studentRecords.StudentID == StudentID select studentRecords).FirstOrDefault(); // remove the selected student from the db db.Students.Remove(deletedStudent); // save my changes back to the db db.SaveChanges(); // refresh the grid this.getStudents(); } }
public IEnumerable <Employee> GetEmployees() { using (var dbContext = new ContosoContext(this.contosoType)) { return(dbContext.Employees.OrderBy(e => e.FirstName).ToList()); } }
protected void StudentsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store which row was clicked int selectedRow = e.RowIndex; //get the selected StudentID using the grids DataKey collection int StudentID = Convert.ToInt32(StudentsGridView.DataKeys[selectedRow].Values["StudentID"]); //use EF and Ling to fidn the selected student in the db and remove it using (ContosoContext db = new ContosoContext()) { //create and object of the student class and store th query inside of it Student deletedStudent = (from studentRecords in db.Students where studentRecords.StudentID == StudentID select studentRecords).FirstOrDefault(); //remove the selected student from the db db.Students.Remove(deletedStudent); //save my changes back tot he database db.SaveChanges(); //refresh the grid this.GetStudents(); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); services.AddCors(options => { options.AddPolicy(name: MyPolicy, builder => { builder .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() .SetIsOriginAllowed((host) => true); }); }); var db = new ContosoContext(new DbContextOptionsBuilder <ContosoContext>() .UseSqlite($"Data Source={_appHost.ContentRootPath}/Contoso.db").Options); services.AddScoped <ICustomerRepository, SqlCustomerRepository>(_ => new SqlCustomerRepository(db)); services.AddScoped <IOrderRepository, SqlOrderRepository>(_ => new SqlOrderRepository(db)); services.AddScoped <IProductRepository, SqlProductRepository>(_ => new SqlProductRepository(db)); services.AddControllers(); }
// GET: Instructor public ActionResult Index() { ContosoContext cc = new ContosoContext(); InstructorRepository ir = new InstructorRepository(cc); InstructorService instructorService = new InstructorService(ir); var instructor = instructorService.GetAllInstructors(); return(View(instructor)); }
public IHttpActionResult DeleteStudent(int id) { _context = new ContosoContext(); var student = _context.Students.Find(id); _context.Students.Remove(student); _context.SaveChanges(); return(Ok(student)); }
public static bool ReqDelCategory(long id) { using (var context = new ContosoContext()) { var category = context.Categories.Single(c => c.CategoryID == id); context.Categories.Remove(category); context.SaveChanges(); return(!context.Categories.Any(c => c.CategoryID == id)); } }
public static bool ReqDelSupplier(long id) { using (var context = new ContosoContext()) { var supplier = context.Suppliers.Single(s => s.SupplierID == id); context.Suppliers.Remove(supplier); context.SaveChanges(); return(!context.Suppliers.Any(s => s.SupplierID == id)); } }
private void getStudents() { using (ContosoContext db = new ContosoContext()) { var students = (from allstudents in db.Students select allstudents); StudentGridView.DataSource = students.ToList(); StudentGridView.DataBind(); } }
public static bool ReqDelProduct(long id) { using (var context = new ContosoContext()) { var product = context.Products.Single(p => p.ProductID == id); context.Products.Remove(product); context.SaveChanges(); return(!context.Products.Any(p => p.ProductID == id)); } }
public Employee GetEmployee(Guid employeeId) { using (var dbContext = new ContosoContext(this.contosoType)) { var empQuery = from emp in dbContext.Employees where emp.EmployeeId == employeeId select emp; return(empQuery.FirstOrDefault()); } }
public static bool ReqUpdateCategory(CategoryDetail cd) { using (var context = new ContosoContext()) { var category = context.Categories.Single(s => s.CategoryID == cd.CategoryId); category.CategoryName = cd.CategoryName; category.Description = cd.Description; category.Picture = cd.Picture; context.SaveChanges(); return(true); } }
private void getStudents() { //connect to Entity Framework Database using (ContosoContext db = new ContosoContext()) { //query the student data var studnets = (from allStudents in db.Students select allStudents); // bind the resultset to the student grid StudentGridView.DataSource = studnets.ToList(); StudentGridView.DataBind(); } }
private void GetData() { //connect to DB using (TodoContext db = new ContosoContext()) { //querry the todoList table var Data = (from alldata in db.Students select alldata); //bind the result to the TodoList GridView TodoListGridView.DataSource = TodoList.ToList(); TodoListGridView.DataBind(); } }
private void getDepartment() { //connect to Entity Framework Database using (ContosoContext db = new ContosoContext()) { //query the department data var department = (from alldepartment in db.Departments select alldepartment); // bind the resultset to the student grid DepartmentGridView.DataSource = department.ToList(); DepartmentGridView.DataBind(); } }
public static List <CategoryDetail> ReqGetCategories() { using (var context = new ContosoContext()) { var categories = context.Categories; return(categories.Select(c => new CategoryDetail { CategoryName = c.CategoryName, Description = c.Description, CategoryId = c.CategoryID, Picture = c.Picture }).ToList()); } }
public static CategoryDetail ReqGetCategoryById(long id) { using (var context = new ContosoContext()) { var cat = context.Categories.Single(c => c.CategoryID == id); return(new CategoryDetail() { CategoryName = cat.CategoryName, Description = cat.Description, CategoryId = cat.CategoryID, Picture = cat.Picture }); } }
public LogService() { // Uses the configuration file to get the default primary key reference from the table bank. var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); // It is assumed that your tables use the name column name for primary key. _key = config.GetValue <string>("KeyModel"); // Initialize context _db = new ContosoContext(); }
/// <summary> /// This method gets the student data from db /// </summary> private void GetStudents() { //connect to EF DB using (ContosoContext db = new ContosoContext()) { //query the student table using EF and LINQ var Students = (from allStudents in db.Students select allStudents); //bind the result to the Students GridView StudentsGridView.DataSource = Students.ToList(); StudentsGridView.DataBind(); } }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> public void ConfigureServices(IServiceCollection services) { var db = new ContosoContext(new DbContextOptionsBuilder <ContosoContext>() .UseSqlServer(Constants.SqlAzureConnectionString).Options); services.AddScoped <ICustomerRepository, SqlCustomerRepository>(x => new SqlCustomerRepository(db)); services.AddScoped <IOrderRepository, SqlOrderRepository>(x => new SqlOrderRepository(db)); services.AddScoped <IProductRepository, SqlProductRepository>(x => new SqlProductRepository(db)); services.AddMvc(); }
public static List <SupplierType> ReqGetSupplierByName(string name) { using (var context = new ContosoContext()) { var suppliers = context.Suppliers.Where(s => s.CompanyName.Contains(name)); return(suppliers.Select(s => new SupplierType { CompanyName = s.CompanyName, ContactName = s.ContactName, ContactTitle = s.ContactTitle, Country = s.Country, SupplierId = s.SupplierID }).ToList()); } }
public static bool ReqAddCategory(CategoryDetail cd) { using (var context = new ContosoContext()) { var category = new Categories { CategoryID = cd.CategoryId, CategoryName = cd.CategoryName, Description = cd.Description, Picture = cd.Picture }; context.Categories.Add(category); context.SaveChanges(); return(true); } }
public void DeleteEmployee(Guid employeeId) { //if (this.contosoType == ConnectionType.Client_MySql || this.contosoType == ConnectionType.Client_Http_MySql) //{ // var settingsHelper = ContainerHelper.Current.Container.Resolve<SettingsHelper>(); // var csString = settingsHelper[contosoType]; // try // { // using (var connection = new MySqlConnection(csString)) // { // connection.Open(); // string commandtext = "DELETE FROM employees where employeeid = @employeeid"; // MySqlCommand command = new MySqlCommand(commandtext, connection); // command.Parameters.AddWithValue("@employeeId", employeeId); // command.ExecuteNonQuery(); // connection.Close(); // } // } // catch (Exception) // { // throw; // } // return; //} using (var dbContext = new ContosoContext(this.contosoType)) { var empQuery = from emp in dbContext.Employees where emp.EmployeeId == employeeId select emp; var employee = empQuery.FirstOrDefault(); if (employee == null) { return; } dbContext.Employees.Remove(employee); dbContext.SaveChanges(); } }
public Employee GetEmployee(Guid employeeId) { //if (this.contosoType == ConnectionType.Client_MySql || this.contosoType == ConnectionType.Client_Http_MySql) //{ // var settingsHelper = ContainerHelper.Current.Container.Resolve<SettingsHelper>(); // var csString = settingsHelper[contosoType]; // try // { // using (var connection = new MySqlConnection(csString)) // { // connection.Open(); // string commandtext = "SELECT * employees where employeeid = @employeeid"; // MySqlCommand command = new MySqlCommand(commandtext, connection); // command.Parameters.AddWithValue("@employeeid", employeeId); // Employee employee = null; // using (var reader = command.ExecuteReader()) // { // reader.Read(); // employee = GetEmployeeFromReader(reader); // } // connection.Close(); // return employee; // } // } // catch (Exception) // { // throw; // } //} using (var dbContext = new ContosoContext(this.contosoType)) { var empQuery = from emp in dbContext.Employees where emp.EmployeeId == employeeId select emp; return(empQuery.FirstOrDefault()); } }
public static bool ReqUpdateProduct(ProductDetail pd) { using (var context = new ContosoContext()) { var product = context.Products.Single(p => p.ProductID == pd.ProductId); product.CategoryID = pd.CategoryId; product.Discontinued = pd.Discontinued; product.ProductID = pd.ProductId; product.ProductName = pd.ProductName; product.QuantityPerUnit = pd.QuantityPerUnit; product.ReorderLevel = pd.ReorderLevel; product.SupplierID = pd.SupplierId; product.UnitPrice = pd.UnitPrice; product.UnitsInStock = pd.UnitsInStock; product.UnitsOnOrder = pd.UnitsOnOrder; context.SaveChanges(); return(true); } }
public static bool ReqUpdateSupplier(SupplierDetail sd) { using (var context = new ContosoContext()) { var supplier = context.Suppliers.Single(s => s.SupplierID == sd.SupplierId); supplier.Address = sd.Address; supplier.City = sd.City; supplier.Fax = sd.Fax; supplier.Phone = sd.Tel; supplier.Region = sd.Region; supplier.CompanyName = sd.CompanyName; supplier.Country = sd.Country; supplier.ContactTitle = sd.ContactName; supplier.HomePage = sd.Website; supplier.ContactName = sd.ContactName; context.SaveChanges(); return(true); } }
public void DeleteEmployee(Guid employeeId) { using (var dbContext = new ContosoContext(this.contosoType)) { var empQuery = from emp in dbContext.Employees where emp.EmployeeId == employeeId select emp; var employee = empQuery.FirstOrDefault(); if (employee == null) { return; } dbContext.Employees.Remove(employee); dbContext.SaveChanges(); } }